Eloi Navarro
Eloi Navarro

Reputation: 1445

Android finishing previous activities for navigation

I have 5 activities (let's say A,B,C,D and E). Activities A to D are for setting some data, so user may be able to go back and forth changing whatever. Activity E, on the other hand, is the summary, so the user is no longer allowed to go back.

My idea is to finish all previous activities when user gets to E

Currently my activities A to D have no flags attached to them.

Activty E is called like this:

Intent I = new Intent(this, SomeClass.class);

I.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

startActivity(I);

I have read everywhere that those flags are supposed to achieve what I am trying to do, but they doesn't, when user gets to activity E and presses Back button, the application goes back to activity D.

Any ideas? I am missing something? Is this not the right way to solve this problem?

Thanks for your help

Upvotes: 5

Views: 6340

Answers (5)

fernandospr
fernandospr

Reputation: 3051

Just in case someone needs the code that @achie suggested, here it is:

A,B,C,D activities should start the following activity like this:

Intent myIntent = new Intent(this, B.class); // Or C/D
startActivityForResult(myIntent, 1);

Also, A,B,C,D activities should override:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   if (resultCode == RESULT_OK && requestCode == 1) {
      setResult(RESULT_OK);
      finish();
   }
}

Then E activity should handle back button:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
   if (keyCode == KeyEvent.KEYCODE_BACK) {
      setResult(RESULT_OK);
   }
   return super.onKeyDown(keyCode, event);
}

Upvotes: 5

MarkySmarky
MarkySmarky

Reputation: 1629

I suppose your're trying to write some sort of a Wizard :)

In order to use the FLAG_ACTIVITY_CLEAR_TOP flag you must have activity E already in current activities task.

Example: E->A->B->C->D --> if D will start E with CLEAR_TOP you will have only activity E in stack.

FLAG_ACTIVITY_CLEAR_TOP Using flag CLEAR_TOP to launch an activity If there is already an instance of the called activity type present in the stack, then this instance is brought to the foreground instead of creating a new instance. Also, all activities in the stack that reside on top of that instance are cleared from the stack. For example, assuming that the current activity stack is ABCDE, launching an activity of type C will clear activities D and E from the task and result in the stack ABC.

FLAG_ACTIVITY_SINGLE_TOP (not relevant in your scenario) If an instance of the target activity is already at the top of the stack, no new instance is created.

Upvotes: 3

achie
achie

Reputation: 4746

Use the method startActivityForResult in each of the activities A, B, C, D instead of startActivity. And when the user exits out of Activity E, handle the backbutton and set the result code in the intent. now in the onActivityResult method of each A, B, C and D classes handle it , close the activity and pass the result to previous activity.

You can also do it as soon as user finishes Activity D. When users go from D to E, you may use the above method to actually go all the way down to Activity A and then open activity E and finish Activity A.

Both the above cases produce the same result from users perspective and they do not see the intermediate activities A, B, C, D unless you are doing some long jobs in the onStart method of those activities.

Upvotes: 7

Buda Gavril
Buda Gavril

Reputation: 21657

when you start activity E, use this code:

Intent I = new Intent(this, SomeClass.class);
I.putExtra("finish_all", true);
I.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(I);

Upvotes: 0

neevek
neevek

Reputation: 12148

You need to override the onKeyDown() method in Activit E and intercept the key press event for KEYCODE_BACK(the Back key), and simply return true to indicate you handled the Back event. Now the user will not be allowed to navigate back to the previous activity.

Upvotes: 0

Related Questions