Skip to content Skip to sidebar Skip to footer

Activity In Background Gets Killed When Home Button Is Pressed

I encountered strange problem, lets say I have two activities A and B, app starts with Activity A, I proceed to activity B press Android Home Button, return to app which brings me

Solution 1:

The behaviour of back buttons depends on system version. There is support for providing back navigation in older Android versions, described here:

https://developer.android.com/training/implementing-navigation/ancestral.html

<application... >
    ...
    <!-- The main/home activity (it has no parent activity) --><activityandroid:name="com.example.myfirstapp.MainActivity"...>
        ...
    </activity><!-- A child of the main activity --><activityandroid:name="com.example.myfirstapp.DisplayMessageActivity"android:label="@string/title_activity_display_message"android:parentActivityName="com.example.myfirstapp.MainActivity" ><!-- Parent activity meta-data to support 4.0 and lower --><meta-dataandroid:name="android.support.PARENT_ACTIVITY"android:value="com.example.myfirstapp.MainActivity" /></activity></application>

The best and most convenient way to debug back stack issues is to enable 'Don't keep activities' option in developer options.

That's my best guess. Good luck!

Solution 2:

In order to run a new activity without destroying the old one, you have to add the flag FLAG_ACTIVITY_NEW_TASK to the intent that will run the activity:

 Intent intent = newIntent(MainActivity.this, MainActivity2.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

as when setting this flag:

this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. Tasks can be moved to the foreground and background; all of the activities inside of a particular task always remain in the same order.

so the activity which started it will remain in the stack, and hence you can call it again, and hence it also can be called automatically again when pressing BACK_BUTTON even if you pressed the HOME_BUTTON earlier.


and you have to combine @gduh answer with mine, as for sure you must make sure that you are not calling finish(); in ActivityA while calling the ActivityB.

Solution 3:

thanks for help, problem was caused by this flag for activity in manifest android:launchMode=singleinstance (it's not originally my project so I missed that, I just hope I didn't screw something else up by removing it)

Solution 4:

In your activity A when you call your activity B, maybe you have the following command :

finish();

If yes, you should remove this line. Then when you press back key from your activity B you should return A. If not, maybe try to share your code.

Post a Comment for "Activity In Background Gets Killed When Home Button Is Pressed"