Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does FLAG_ACTIVITY_CLEAR_TOP not work?

As the title says, Why intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) or intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) won't work?

I have 3 Activities let us say A, B and C.

When I am trying to launch Activity A from C with code:

Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

It simply starts Activity A but doesn't clear the top.! -_-

I have also tried using setFlags().

I read different questions on SO about this problem, but I couldn't find the right answer. >_<

Somebody please help!

Edit

Code for onBackPressed() in activity 'A' as requested by @codeMagic.

@Override
public void onBackPressed(){
    if(wvLogin.canGoBack())
        wvLogin.goBack();
    else
        super.onBackPressed();
}
like image 523
ashu Avatar asked May 18 '14 03:05

ashu


People also ask

What is Flag_activity_clear_top?

flag — FLAG_ACTIVITY_CLEAR_TOP: If the Activity being started is already running in the current task then instead of launching the new instance of that Activity, all the other activities on top of it is destroyed (with call to onDestroy method) and this intent is delivered to the resumed instance of the Activity (now ...

What are intent flags in Android?

Use Intent Flags Intents are used to launch activities on Android. You can set flags that control the task that will contain the activity. Flags exist to create a new activity, use an existing activity, or bring an existing instance of an activity to the front.

What is intent Flag_activity_new_task?

The flags you can use to modify the default behavior are: FLAG_ACTIVITY_NEW_TASK. Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent() .


2 Answers

From the documentation for FLAG_ACTIVITY_CLEAR_TOP:

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.

As you added in your comment, the activity A has been finished before calling B, so this situation doesn't apply. A new instance of activity A will be launched instead.

As I see it, you have two options here:

1) Use the Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK flags. This will start activity A as the root of the stack. It works, but any other activities in the stack will be lost. Assuming A was the first activity (or at least, that you are not interested in any previous activities in the task stack) then it doesn't matter. Note: CLEAR_TASK requires API Level 11.

2) Another possible solution (in case the previous assumption is not true) would be to not use intent flags at all:

  • B starts C with startActivityForResult().
  • Instead of calling A, C finishes, having set a result for B indicating that A must be launched.
  • In B.afterActivityResult(), finish B and launch A.
like image 150
matiash Avatar answered Oct 13 '22 14:10

matiash


You are missing the Intent.FLAG_ACTIVITY_SINGLE_TOP flag

Try this:

Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
like image 9
goodKode Avatar answered Oct 13 '22 13:10

goodKode