Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear all activities except one in android

Below I'm showing the flow on how I want to navigate my Activities:

enter image description here

I tried writing the following code inside D and E:

Intent list = new Intent(AddComplaint.this, B.class);
list.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(list);

However, I'm facing two issues:

  1. When B gets launched it shows a grey screen for a while, then it gets loaded.
  2. When I go back from B it exits out of the application rather than going to A (the dashbord).

How can I fix this?

like image 546
Hunt Avatar asked Oct 18 '22 09:10

Hunt


1 Answers

I believe you can achieve what you want by using FLAG_ACTIVITY_CLEAR_TOP. If you send an Intent using this flag, it will be delivered to the existing Activity B, and any activities above B on the stack (C, D/E) will be finished.

Using FLAG_ACTIVITY_CLEAR_TASK will finish all previous activities on the stack, which would make B the only remaining activity - explaining why you exit the app when clicking back. Your grey background is unrelated to activity management and indicates the activity is simply taking a while to call onCreate().

Example code:

Intent list = new Intent(AddComplaint.this, B.class);
        list.setFlags
                (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(list);
like image 122
fractalwrench Avatar answered Oct 21 '22 06:10

fractalwrench