0

I am new to Android . Here I have for activities A,B,C,D in which A is the Home Activity.It is in stack as A->B->C->D

When I press back from B or C it should go back just as normal. But if I press back from D it should go back to A and from A the app should exit

Nidhin Rejoice
  • 1,367
  • 1
  • 16
  • 26

2 Answers2

1

I guess you could intercept onStop() and guessing if the activity is switching to C and launching A instead. But it would result in a hard to maintain mess and I do not recommend that.

However, if for some reason you still not want to override onBackPressed and you manage to guess that D is stopping because back was pressed (without overriding onBackPressed(), just start A activity from there with an Intent with FLAG_ACTIVITY_CLEAR_TOP (call i.setFlags(FLAG_ACTIVITY_CLEAR_TOP) )

According to the doc:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

So A will be brought back and B and C will be cleared.

fedepaol
  • 6,834
  • 3
  • 27
  • 34
0

when you start your new activity finish the old activity for example if you want to start C from B :

Intent I = new Intent(B.this , C.class) ;
 startActivity (I);  
B.this.finish();
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
Developer So far
  • 353
  • 1
  • 11