Problem with stack problem

I have two sets of actions, presumably 3 actions of each set, (A1, B1, C1 || A2, B2, C2). I run my application from A1, then β†’ B1 β†’ C1 here. I want to switch from C1 to β†’ A2 and in A2, if I press back, there must be an application and not return me for C1, and then from A2 go to β†’ B2 β†’ C2.

So basically I want to change the initial activity, it looks like I have two applications in one application, and when I switch to the second application, I need to clear the activity stack. Is it possible? Any ideas?

+4
source share
7 answers

It seems you answered your question. You wrote:

So basically I want to change the initial activity, it looks like I have two applications in one application, and when I switch to the second application, I have to clear the activity stack.

I would do it like this:

Create a DispatcherActivity , which is the activity that starts when the application starts. This activity is the main activity of your task and is responsible for starting both A1 and A2 depending ... and DO NOT call finish() on its own (that is: it will be covered by A1 or A2, but it will still be at the root of the stack activity )

In A1 , hook the back key and tell DispatcherActivity to complete as follows:

 @Override public void onBackPressed() { Intent intent = new Intent(this, DispatcherActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addExtra("exit", "true"); startActivity(intent); } 

This will clear the task stack to root activity ( DispatcherActivity ), and then run DispatcherActivity again with that intention.

In C1 , to start A2 , follow these steps:

 Intent intent = new Intent(this, DispatcherActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addExtra("A2", "true"); startActivity(intent); 

This will clear the task stack to root activity ( DispatcherActivity ), and then run DispatcherActivity again with that intention.

In DispatcherActivity , in onCreate() you need to determine what to do based on additional functions in the intent, for example:

 Intent intent = getIntent(); if (intent.hasExtra("exit")) { // User wants to exit finish(); } else if (intent.hasExtra("A2")) { // User wants to launch A2 Intent a2Intent = new Intent(this, A2.class); startActivity(a2Intent); } else { // Default behaviour is to launch A1 Intent a1Intent = new Intent(this, A1.class); startActivity(a1Intent); } 

In A2 hook the back key and tell DispatcherActivity to exit using the same onBackPressed() override as in A1 .

Note. I just typed this code, so I did not compile it, and it may not be ideal. Your mileage may vary; -)

+1
source

if you want to close the application when you press the back button instead of going back, you must overwrite the back button. in the method to complete the completion of the method () call after the operation is closed. I hope this can help you.

edited by:

check the link below: this is to close all actions in the view stack. before closing the application, close all actions.

http://www.coderzheaven.com/2011/08/27/how-to-close-all-activities-in-your-view-stack-in-android/

0
source

You can check when the button pressed in step A2, and then if its back button, you can close the application. You can use the following method in A2

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { this.finish(); } return super.onKeyDown(keyCode, event); } 
0
source

try to start operation A2 with this intent - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

0
source

Handle this with the onBackPressed and finish methods.

Before starting another operation, it is better to close the current activity using the finish () method.

If you want to go back to the previous step, override the onBackPressed method and call the specific intent.

In step A2, add the completion method to the onBackPressed method (do not call the previous activity here). This is one way.

0
source

when transferring Intent from Activity C1 to A2 use

 Intent intent=new Intent(C1.this,A2.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 

and in your activity A2 Override the back button

  @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { A2.this.finish(); } return super.onKeyDown(keyCode, event); } 
0
source

FLAG_ACTIVITY_CLEAR_TOP is not what you are looking for. I think you are looking for:

 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
0
source

All Articles