TheJudge
TheJudge

Reputation: 606

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 back to Activity B. Then I press Back button (either hardware on in toolbar) and this closes app, but it should return me to Activity A. Activity B has no override of onBackPressed and has Activity A stated as PARENT_ACTIVITY in manifest. I'm starting it with Intent with no flags. Any idea why this happens ? Thanks

Upvotes: 0

Views: 1765

Answers (4)

TheJudge
TheJudge

Reputation: 606

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)

Upvotes: 1

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

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 = new Intent(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.

Upvotes: 1

pawelo
pawelo

Reputation: 1415

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) -->
    <activity
        android:name="com.example.myfirstapp.MainActivity" ...>
        ...
    </activity>
    <!-- A child of the main activity -->
    <activity
        android: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-data
            android: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!

Upvotes: 1

gduh
gduh

Reputation: 1187

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.

Upvotes: 0

Related Questions