3

In my menu I have some items. Home is an item of it that I want to be root of my application and whenever user clicks on it, Android clear stack and then come back to main screen.

This is my code:

menu.setOnItemClickedListener(new MenuClickedListener() {
            public void eventOccured(int id) {          

                Intent intent = null;
                switch(id) {
                    case 1: intent = new Intent(context, More.class);           break;
                    case 2: intent = new Intent(context, FavoriteScreen.class); break;
                    case 3: intent = new Intent(context, VideoShowList.class);  break;
                    case 4: intent = new Intent(context, ShoppingList.class);   break;
                    case 5: intent = new Intent(context, MainScreen.class); 
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            break;                  
                }       

                if(intent != null)
                    context.startActivity(intent); 
            }
        });

Menu works fine but it seems flag doesn't work because from Home screen, i go to second and third screen then i click on Home item of menu and come back to home screen. Now, when i click on back button, i go to third screen, second screen and Home screen.

I have designed this menu as widget in order to setup it one time and reuse it on all of my screens.

Any suggestions would be appreciated. Thanks.

Hesam
  • 49,622
  • 70
  • 212
  • 354

6 Answers6

20

I have tried Intent.FLAG_ACTIVITY_CLEAR_TOP But haven't got proper solution so finally this helps me


Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);

This clears all the stack and run only LoginActivity

Bhavin Chauhan
  • 1,890
  • 1
  • 25
  • 46
18

Read it once and you will never have the problem in the usage of flag_activity_clear_top

Here is a simple clarification regarding the usage of Flag_Activity_Clear_Top.

One Liner Formula for its right use :

It always clears the intermediate Activities ( if any ) between the calling activity and the called activity.

Here is an Activity Flow :

Activity A -> Activity B -> Activity C ( use flag activity clear top to go to D) ->Activity D

In the above sequence when reaching to D if we press back then we will get Activity C->Activity B-> Activity A , as there is no INTERMEDIATE ACTIVITY between Activity C and Activity D

Myth of some developers here is -

After reaching the activity D by using flag activity clear top from C, they were expecting that on the press of back key from D will take them to outside the application., which will never happen.

Flag_Activity_Clear_Top always always clears the INTERMEDIATE ACTIVITIES BETWEEN THE TWO ACTIVITIES IF ANY

Gaurav Arora
  • 8,092
  • 20
  • 85
  • 136
  • 7
    Good explanation. Would be even better if you would include the solution for what everybody wants - make activity the only one left in the back stack. – Jaroslav Oct 23 '14 at 13:59
7

Try using this intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); inplace of intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent intent = new Intent(getApplicationContext(),
            yourActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    startActivity(intent);
Rahul Patel
  • 3,803
  • 5
  • 29
  • 46
3

If you want to start an Activity to be the only one existing (that is, clear the whole backstack), use the following flags:

Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK

Requires API-Level >= 11.

Salim
  • 2,117
  • 1
  • 12
  • 12
1

I faced the same issue, my problem was that activity in intent has launchMode="singleInstance". changed to singleTask and code worked as it should.

-3

maybe this works :

intent = new Intent(getApplicationContext(), MainScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Habib Zare
  • 1,166
  • 7
  • 17