Android: Clear Activity History

2019-08-30 10:36发布

问题:

I've faced with one problem - clearing activity history. I have the following consequence where my problem appears:

A(SplashScreen)->B(GuestScreen)->C(Screen for signed users)->Press Home Button->A->B->Press Back Button->C->Press Back BUtton->B->Press Back Button->exit from the app.

I tried different ways to solve it and found only one quite strange solution throughout setting almost all activities to singleInstance and setting all intent flags to CLEAR_TOP and NEW_TASK but all activities are presenting some strange(not as normally, I mean, firstly, there appears empty black screen with actionBar and after ~1 second appears my real layout) and with a bit delay as I already said. I didn't like this solution in spite of that everything closes correctly and in correct consequences. Now, I'm trying to get it work normally without this delay and strange black screen on a moment. I tried to play with intent flags like CLEAR_TASK, CLEAR_TOP too. Tried to use clearTaskOnLaunh for root activity and finishOnLaunch in manifest file for others where it`s necessary, doesn't help. How to fix it?

EDIT:

Trying set clearTaskOnLaunch to A again. I see next log when start app first time(A has already called B without any flags):

  Main stack:
    TaskRecord{42610648 #869 A }
    Intent { flg=0x10000000 cmp=/.activities.SplashActivity }
      Hist #4: ActivityRecord{413cf8f8 /.activities.GuestActivity}
        Intent { cmp=/.activities.GuestActivity }
        ProcessRecord{414fdf60 15651:/10103}

// Guest Activity(B) calls UserActivity(C):

    Intent { flg=0x10000000 cmp=/.activities.SplashActivity }
  Hist #3: ActivityRecord{415c86f0 /.activities.UserActivity}
    Intent { cmp=/.activities.UserActivity }
    ProcessRecord{414fdf60 15651:/10103}

//Press Home Button and launch app again(A has called B):

    TaskRecord{42610648 #869 A cashongo.app.peachy}
Intent { flg=0x10000000 cmp=c/.activities.SplashActivity }
  Hist #5: ActivityRecord{42127398/.activities.GuestActivity}
    Intent { cmp=/.activities.GuestActivity }
    ProcessRecord{414fdf60 15651:/10103}
  Hist #4: ActivityRecord{415c86f0 /.activities.UserActivity}
    Intent { cmp=/.activities.UserActivity }
    ProcessRecord{414fdf60 15651:/10103}

The same problem again, last activity in history. If I press back button I`ll get UserActivity.

回答1:

I didn't understand your question 100%

I think you are facing problem with activity stack and CLEAR_TOP

so use startActivityForResult instead of startActivity



回答2:

The way we fixed it is taking a static boolean variable in the immediate first activity .Say your first activity is A,In A declare variable as below

public static boolean closeAllActivities=false;

Then in rest of all activities place the below code in onResume()

protected void onResume() {
super.onResume();
if(A.closeAllActivities)
{       
    finish();   
}
else
{
    // TODO your requirement inside onResume
}
      }

And wherever u would like to exit the application set as

A.closeAllActivities=true;//It exits all the activities one after other.


回答3:

It seems I have managed to fix it but it is still a bit strange. I have set launchMode=singleInstance to A and B activities. And call any activities without any flags. No black screen with 1 second delay between activity`s calling. A bit strange solution because google not recommend use singleInstance or singleTask for general situations, I think it's general. If someone will find a better solution you are welcome.

UPDATE: One more very important thing when build and start apk via IDE. Some kind of IDE start app wrongly see this post. Due to it stack behaviour can be changed.