0

Assume that I have a launcher activity A which has singleTask launch mode. Now imagine that A starts Activity B like;

        Intent intent = new Intent(this, B.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivityForResult(intent, REQ_ACCOUNT_ACTIVITY);

In this case it seems like A's not destroyed. I wonder if it's correct or did I something wrong?

stdout
  • 2,471
  • 2
  • 31
  • 40
  • Don't use CLEAR_TOP with SINGLE_TOP at the same time. – Sumit Uppal Feb 02 '15 at 13:00
  • Hi back. I actually wondered if such a behaviour is OK or not. I mean afterall A is the root activity and sort of the task owner. Trying to clear it from B in the way I did above is not destroying the A. It may be the correct attitude for the app but I need a verification for this. (like a ref. to official document pointing this issue) – stdout Feb 03 '15 at 07:52
  • [Android API Document](http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK) – Rupesh Yadav Feb 14 '15 at 12:45

2 Answers2

0

You can achieve this by using below attribute in AndroidMenifest file

android:finishOnTaskLaunch="true"

 <application
   ...
   >
    <activity
        android:finishOnTaskLaunch="true"
        android:launchMode="singleTask" 
        ...>
    </activity>
</application>

If you not want to kept new activity in the history stack. Use below one_

FLAG_ACTIVITY_NO_HISTORY

//Actvity B is not in BackStack if we set FLAG_ACTIVITY_NO_HISTORY flag
 Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, REQ_ACCOUNT_ACTIVITY);

As soon as the user navigates away from Activity B, the activity is finished. This may also be set with the noHistory attribute.

For more -> Tasks and Back Stack

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
  • Hi. Thank you for the answer but I was actually asking if the singleTask activity could be destroyed via CLEAR_TOP flag. Assume that stack has A, B, C activities from down to bottom. A is the singleTask activity. Now assume that C started activity A in somewhere and A is taken to foreground and stack becomes like B, C, A. Here is what I wanted to point out - Now A is started B with CLEAR_TOP which should destroy A & C activities. But as far as I see, A ( my singleTask activity ) is not destroyed. Does it make sense? – stdout Feb 15 '15 at 12:25
0

It's been a while but I came across my own question when I was wandering over here.

The activity A was not getting deleted because the flags are set for the activity B. And when I call this code, there's no such activity called B in the task stack already. So effectively, nothing to clear on top of B.

Hope this helps.

stdout
  • 2,471
  • 2
  • 31
  • 40