Search code examples
androidandroid-activityandroid-homebutton

activity closes when home button pressed


I have 2 activities "A" and "B", "A" starts "B". I start activity "B" with startActivityForResult() method. When "B" is in foreground and home button is pressed app goes to background. And when I try to launch app again it starts with activity "A", not "B". Result for activity "B" is cancelled. Why "B" is closed? I thought that using launchModes in manifest can solve the problem but documentation says that I can't use singleTask and singleInstanse modes becouse the result will be cancelled and activity will not start.

How can I solve this?

PS Thanks for answers, problem is solved. I tried to use finish() in onStop(), so when activity "B" is in foreground and home button is pressed, it finishes.


Solution

  • Start your activity B with an intent, using the method startActivity(Intent).

    Your intent should be created with the arguments A.this and B.class, resulting in code looking somewhat like this:

    Intent i = new Intent(A.this, B.class);

    A.this.startActivity(i);