0

I have activity A->B in the stack, and to launch activity C, I call

        Intent starter = new Intent(context, MainActivity.class);
    starter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    context.startActivity(starter);

This all works fine, Activity A and B both have OnDestroy called. If I press the hardware 'back' button now, the activity appropriately finishes and is hidden. The problem is now however, if I return to the application through the application by clicking the hardware recent apps button, it will return to Activity A. Activity was destroyed and not in the stack. In the manifest, none of activities have had a android:launchMode set, so they are on default.

The only other possible piece of relevant information is that there is an Activity X that is a launcher Activity that is android:launchMode="singleInstance" and it launches activity A, that being said, it gets destroyed and it shouldn't be in that activity stack anyways.

Nic Capdevila
  • 1,495
  • 14
  • 27
  • After reading your description I think that it is a correct behavior. When there is nothing in the stack than of-course the Launcher Activity will be called (which is than calling the Activity A). So can tell me what are you expecting to happen? – AabidMulani Oct 28 '16 at 07:51
  • Sorry if it was a little confusing, but the paragraph at the end explains it. Activity X is actually the launching activity, and is a singleInstance (no other activities can launch in it's task), and it launches activity A and then finishes. So it would make since if activity X was launched again, but that's not the case, activity A is launched. – Nic Capdevila Oct 28 '16 at 15:52

2 Answers2

0

While pressing back button While in Activity C may called onDestroy() of actvity C.

please insert logs to see whether it is called or not. This is the only reason why your activity A launch again.

please refer Android Back button calls ondestroy?

please let me know if these not work for you.

Community
  • 1
  • 1
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • onDestory is being called, but that is what should happen on a backpress at the bottom of an activity stack. If it is the only activity in the stack, it destroys, and when you return to the application, it will call onCreate to that activity again. The problem is, following the pattern I described before, activities A and B are destroyed, – Nic Capdevila Oct 28 '16 at 16:16
  • NO it will call Activity A – Jitesh Mohite Oct 28 '16 at 17:59
  • Why A? The Task has been cleared and a new task created via the flags for activity C. – Nic Capdevila Oct 28 '16 at 19:37
0

The hardware back-button can be overwritten by the follwing code :

    @Override
public void onBackPressed() {
   //put Intent to go back here
 }

You could just overwrite it with your code written above

Pynnie
  • 136
  • 12
  • That's true, but that prevents this activity from being truly modular and independent. If there is another activity below it on the stack, it should just pop up. I am just confused why it's returning to an activity that has already been destroyed and was just previously on the stack, and is not the launcher activity. – Nic Capdevila Oct 28 '16 at 16:02