0

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.

Matthew Carlson
  • 654
  • 5
  • 12

1 Answers1

0

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.

hrushi
  • 294
  • 1
  • 10
  • I can't say for sure, but I am almost certain it is not a fragmentManger issue, because it only seems to happen in Landscape mode. It also has worked many times in the past, we only found the situation when we logout and re-start the flow. – Matthew Carlson May 18 '16 at 22:13
  • Oh...I think you may need to use onConfigurationChanged(). If so you could look here: http://stackoverflow.com/questions/9566633/how-to-use-onconfigurationchanged-and-newconfig-orientation-configuration-o – hrushi May 18 '16 at 22:20
  • I explicitly do not want to use onConfiguratioChanged - I would like my code to be written to correctly handle activity destruction and recreation - However, my activities should not be having any configuration changes in that flow I wrote above. There is no rotation or anything else. – Matthew Carlson May 18 '16 at 22:23