简体   繁体   中英

Sometimes when I finish an activity, it goes to the device home screen, instead of the previous item on the stack

This happens whether I call finish() or use the hardware back button.

This is happening on 4.4.2 x86 AVD emulator, but not physical 5.1.1 device.

This only happens in landscape mode for some reason.

I can recreate this by following the exact steps repeatedly, but I have not been able to create a minimal example yet, trimming out all my companies private information.

I was certain I had seen some reference to this, on stack overflow in the past, but I cannot find it.

I have found one suspect activity launch in my application, that uses getApplicationContext().startActivity(i); with flag FLAG_ACTIVITY_NEW_TASK

The flow that creates this is

  1. Open Application with the table held in landscape. ( Applicaiton opens in MainActivity )
  2. Login ( this is the step that used FLAG_ACTIVITY_NEW_TASK )
  3. Logout ( this calls HomeActivity.startActivity with flag FLAG_ACTIVITY_CLEAR_TOP , and re-starts MainActivity )
  4. Log in again ( this is why I suspect FLAG_ACTIVITY_NEW_TASK , but I CANNOT recreate this in portrait )
  5. Launch a specific activity that goes through fragments
  6. Go through the 4 fragments, and on the 4th one, either press Back or hit the save button that calls finish() ;
  7. The Activity will finish, but the whole application will move to the background.
  8. (At this point, I can re-open the activity form the launcher and it properly resumes at the previous activity )

The only difference I can see using adb shell dumpsys activity activities is one entry that shows when that last fragment is loaded, this value has changed:

Hist #3: ActivityRecord{b4410058 u0 <package>/<DetailActivity> t20} packageName=<package> processName=<package> launchedFromUid=10058 launchedFromPackage=<package> userId=0 app=ProcessRecord{b43f4040 4154:<package>/u0a58} Intent { cmp=<package>/<DetailActivity> (has extras) } **frontOfTask=false** task=TaskRecord{b43ced20 #20 A=<package> U=0 sz=4}

And this also changes mFocusedStack=ActivityStack{b44a83a0 stackId=10, 1 tasks} mStackState=STACK_STATE_HOME_TO_FRONT

As I mentioned, I have tried creating a minimal example, but when I luch all the activities in the same order in a Test App ( that does not do any of my network transactions etc. ), the issue is not visible.

TL:DR; Sometimes in landscape mode, finishing an activity takes me to device home, instead of the previous activity - This is with finish() or the device back button.

And to point out again - this ONLY happens when I combine running the activity 100% in landscape from start to finish ( no rotation ), and when I log out and log back in.

I do not see any items in the log about using too much memory.

As a test to eliminate some recommendations, I did put android:configChanges="keyboardHidden|orientation|screenSize" back on every activity, and I do get the same behavior

I will note that all of our fragments in my application are retained, but I am not getting any errors about out of memory, but it's a thought.

I implemented one for my project. May be this could be of some help to you (note: I used only portrait mode). In my project, I had one activity which triggered multiple fragments. When user presses back button, I wanted to go back in the same order. Here is my implementation.

Add the fragments to backstack:

private void FragmentCommit(FragmentTransaction transaction, Fragment fragment, String fragmentTagName){
transaction
.replace(R.id.main_activity_fragment, fragment, fragmentTagName)
.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
.transaction.addToBackStack(fragmentTagName)
.commit();

}

In the Activity, I overrode onBackPressed():

public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    }
    else {

        FragmentManager fragmentManager = getSupportFragmentManager();

        if(fragmentManager.getBackStackEntryCount()>1){
            fragmentManager.popBackStack();

        }
        else{
            super.onBackPressed();
        }
    }
}

I am checking for > 1 since I always had the first fragment as my home page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM