1

I have been spending lots of hours figuring the reason why top of the stack is not cleared yet.

Well I tried the following:

    Intent intent = new Intent(ActionBarActivity.this, MainActivity.class);

    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    startActivity(intent);

And it turned me to the MainActivity. Then when I try to press back button, the program does not exit, but instead it turns me to the first page of ActionBarActivity.

Let me be more specific:

In MainActivity I call ActionBarActivity. In the ActionBarActivity I have a search bar and I enter a query there and just print the value of the query.

If you think how it will work is a below:

MainActivity -> ActionBarActivity1 -> ActionBarActivity2 -> ActionBarActivity3 -> ..

In that ActionBarActivity I have also an option which brings me back to the MainActivity.

So as I said when I run application with the way above, it will bring me to the MainActivity.

Nice so far, but when I press the back button I expect it to exit, but instead it goes to ActionBarActivity1. I doubt the stack is not properly erased.

What should I do in this case. Thanks

user3764893
  • 697
  • 5
  • 16
  • 33

2 Answers2

0

I'm assuming you've seen this post: Android: Clear the back stack and it is not what you want

The FLAG_ACTIVITY_NEW_TASK is creating an new task stack. When you hit "back" that is the only activity in that stack, so Android goes to the previous task - which has the calling activity at the top.

If you remove that flag, then MainActivity is already in the task stack and is brought to the top. And "back" removes it and brings you back to the calling activity. So far, that is the expected Android behavior.

EDIT: Since you commented that you only have 2 activities, then one option is to "finish()" ActionBarActivity after it starts MainActivity like this:

startActivity(intentMainActivity);
finish();            

To get the behavior you want with many activities or only in certain situations, you need to use SET_RESULT and finish the activities in the stack when you hit the button to go to the MainActivity. In other words, Android assumes you want to keep activities even when new ones start. But in this case you don't, so you have to tell the activity to destroy itself.

i.e. call setResult(DONE) and then use startActivityForResult and check for your "DONE" flag in the onActivityResult method.

then this:

    MainActivity -> ActionBarActivity1 -> ActionBarActivity2 -> ActionBarActivity3 
-> MainActivity

becomes:

    MainActivity -> ActionBarActivity1 -> ActionBarActivity2 -> ActionBarActivity3 
-> call "Done"
    Destroy ActionBarActivity3
    Destroy ActionBarActivity2
    Destroy ActionBarActivity1
    MainActivity remains

For example, in ActionBarActivity2:

startActivityForResult(intentActionBarActivity3, ActionBarActivity2_ID);            

Then in ActionBarActivity3:

public void exitButton(View view) {
    setResult(MY_DONE_FLAG);
    finish();
}

And back in ActionBarActivity2:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ActionBarActivity2_ID && resultCode == MY_DONE_FLAG) {
        setResult(MY_DONE_FLAG, data);
        finish();
    }
}

And so on with all activities that you want to "close" when that button is pressed.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • Yeah I have seen that post. Can you be a bit more specific about your solution – user3764893 Jun 23 '14 at 17:19
  • I think you missunderstood me. `ActionBarActivity1`, `ActionBarActivity2`, `ActionBarActivity3`,... they are same activity. I mean they are `ActionBarActivity`. And I was saying like this only to demonstrate what happens in my code. So there are just two activities. `MainActivity` and `ActionBarActivity`. And as you understand `ActionBarActivity` calls itself – user3764893 Jun 23 '14 at 18:29
  • That simplifies it - the principle is the same, but there may be an easier option. See my edit. – Jim Jun 23 '14 at 18:36
  • So you are saying in my fuction `exitButton(...)` in `ActionBarActivity` I should `setResult` and `finish`. But what about my code above? Should I ignore it? And one more thing you have written `startActivityForResult(intentActionBarActivity3, ActionBarActivity2_ID);` shouldn't it be `startActivityForResult(MainAcitivity, ActionBarActivity);`? Thanks – user3764893 Jun 23 '14 at 18:53
  • Since you only have 2 activities, if you get to ActionBarActivity from MainActivity then the only call you need to make in exitButton(..) is "finish()". As for "startActivityForResult" read this: http://stackoverflow.com/questions/10407159/how-to-manage-start-activity-for-result-on-android - the parameters are the new activity intent and "int" - the requestCode. – Jim Jun 23 '14 at 19:00
0

try

                  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

                  finish();

insetead of

                  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

and you should also use it with every intent which leads you to ActionBarActivity. i mean if you are going back to ActionBarActivity form ActionBarActivity1 then you use it with intent. then create intent for MainActivity in your so called ActionBarActivity.

Intent intent = new Intent(ActionBarActivity.this, MainActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

startActivity(intent);

finish();

Good luck !!!

Sar
  • 550
  • 4
  • 18