2

I have some Activites that are started from my main Activity. When I go to one of these Activities, press home, kill the process, return to the app (where it resumes on Activity I left from), and then press back, it just closes my app instead of going back to my main Activity. I expect the back stack to be preserved. Does anyone know why this is happening?

I'm doing 3 things that might be related:

(1) My main Activity has launch mode singleTask

I tried removing this, but it didn't solve the problem. For example:

<activity
    android:name=".activities.ActivityMain"
    android:launchMode="singleTop"
    android:screenOrientation="portrait">
</activity>

(2) I'm overriding onBackPressed() in the secondary Activity, but calling super.onBackPressed() when I do indeed want to go back. I also tried not doing this, but it also didn't help. For example:

@Override
public void onBackPressed()
{
    Logger.d("ENTER");

    ViewUtils.closeKeyboard(this);

    //kv If it's a valid location, but hasn't changed, then just finish up
    if (((FragmentLocation) getFragment()).isValidSpecifiedLocation()
            && !((FragmentLocation) getFragment()).sendUserSpecifiedLocation())
    {
        finish();
        overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
    }
}

(3) I'm setting the parent Activity in the manifest to my main Activity (I've also tried removing this, but it doesn't help):

<activity
    android:name=".activities.ActivityLocation"
    android:screenOrientation="portrait"
    android:parentActivityName=".activities.ActivityMain">
</activity>

Any ideas?

UPDATE:

I tried recreating a very simple example with these conditions, but couldn't reproduce. I'm not sure what's causing this issue at this point.

Karim Varela
  • 7,562
  • 10
  • 53
  • 78
  • You might have to set your main activity as parent to children activities. Please, take a look at the docs for parent activities and up navigation (http://developer.android.com/training/implementing-navigation/ancestral.html). This is not an answer, just direction of thoughts – krossovochkin May 04 '15 at 12:23
  • Thanks @krossovochkin, tried everything on there, but didn't work. Afaik, the system should restore my back stack for me, not sure why it's not. – Karim Varela May 04 '15 at 12:34
  • One more thing: you have written, that you kill your application? You kill application by 'force close' in the settings? If so, I don't know whether or not system should restore everything as it was before. If you would like to test what will happen when *system* will destroy your activity, then you should set flag in 'Developer Settings' in your phone settings 'Do not keep activities'. More info here: http://stackoverflow.com/questions/22400859/dont-keep-activities-what-is-it-for – krossovochkin May 04 '15 at 12:42
  • 1
    No, I'm not killing the application. I'm killing the process. This is the same thing the Android system does when it's low on memory. I'm just forcing it to mimic those conditions. You do this by backgrounding your app and then pressing the red X in Android Studio. – Karim Varela May 04 '15 at 12:44
  • Cool, didn't know about that feature. And it works good for me. The only thing I could suggest is to build the simplest app with the same basic logic (using only activities without fragments), without overridden methods (e.g. onBackPressed). Ensure that everything works as expected and then add one new feature by one step and recheck again. Good luck :) – krossovochkin May 04 '15 at 13:12
  • Did you ever find anything on this? I'm having the same problem with Fragments. The FragmentManager backstack is cleared on restore, except for the fragments that Android creates for me. So I have about 5 fragments in the backstack, the OS kills the app, and then it starts back on the last fragment with no other fragments in the backstack. I believe this must be by design. Trying to think of a work around, maybe just create my own backstack and not use the built-in backstack (that really is a lot of work though). – reactive-core Nov 18 '15 at 21:23

0 Answers0