0

When I use an intent to move from parent activity to another, and then use action bar nav to go back to parent, the parent activity has lost all my changes and values (as a user) and is back to default xml display i have for it.

I have these in my Child activity OnCreate:

 ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);

And labeled my parent activity appropiately:

android:parentActivityName=".MainActivity"

What do I need to make it store or remember those changes when traveling between activities?

Destreation
  • 105
  • 1
  • 2
  • 12

3 Answers3

1

There are couple of options to try -

1) start the new activity with startActivityForResult()

2) in the manifest file, set the android:launchMode="singleTop" of the parent activity

Hope it helps

SimplyJaymin
  • 444
  • 8
  • 17
1

In the Manifest file, you should set android:launchMode like this:

<!-- ACTIVITY -->
        <activity
            android:name=".activities.MainActivity"
            android:configChanges="orientation|screenSize"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"/>

P.s: read Launch Mode on this link: https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

MrTy
  • 140
  • 1
  • 9
  • Nice! the other guys suggestion of "singletop" launchmode also worked but it think after reading the material you provided that "singleTask" is the most appropriate launch mode. Thanks for an answer with additional info to read :) – Destreation May 26 '17 at 04:03
  • @Destreation Actually, use of `singleTask` launch mode should only be used in very specific cases (like in a HOME-screen replacement), not in standard navigation cases like this. Here the use of `singleTop` should be sufficient to go back to the existing instance of `MainActivity`. `singleTask` launch mode comes with a host of strange behaviour that may cause you a lot of pain in the future. Read [Providing UP navigation](https://developer.android.com/training/implementing-navigation/ancestral.html) – David Wasser May 26 '17 at 13:15
  • @DavidWasser : Can you explain "strange behavior"? – MrTy May 26 '17 at 17:02
  • You can look at many of my other posts regarding `singleTask` and `singleInstance` if you like. `singleTask` makes it impossible for your app to be integrated with other apps, your app cannot have more than one "launch" activity (without causing problems), your app cannot run in more than one task (without causing problems). The implications of using `singleTask` and `singleInstance` launch modes are not well documented but there are hundreds of problems reported from users who use these special launch modes because they do not understand these implications. Since it isn't necessary and ... – David Wasser May 26 '17 at 19:27
  • ...isn't recommended, there really is no reason to use it. Using `singleTop` launch mode will do exactly what OP wants using a "standard" launch mode. – David Wasser May 26 '17 at 19:28
  • Tks for your information Dear @DavidWasser I think using singleTop or singleTask is based on purpose, I have a lot of problems using singleTop with deeplink, it will be on the Task of the application containing deeplink rather than opening the app as usual so I choose SingleTask Tks. – MrTy May 27 '17 at 06:34
  • If you are having issues with multiple tasks you should understand how `taskAffinity` works and use that along with standard launch modes instead. – David Wasser May 27 '17 at 06:50
  • Tks @DavidWasser I tried to use taskAffinity, But it's not work in my case.When using deepLink from another application, when opening the app again, in Task Manager will display 2 apps. And often mainActivity should not be used standard because sometimes need to reopen that screen or avoid the garbage collected to destroy activities. – MrTy May 27 '17 at 07:02
  • we have some discuss that : https://stackoverflow.com/questions/25773928/setting-launchmode-singletask-vs-setting-activity-launchmode-singletop – MrTy May 27 '17 at 07:06
  • A comment thread in this answer probably isn't the right place to discuss this. I'm sure that whatever your problem is, it can be solved using standard launch modes. If you really want to discuss it in detail, please open a new question with the problem you had and we can discuss it there. I would need to see some code and your manifest and have you explain your problem in detail. – David Wasser May 27 '17 at 15:26
0

You might be creating a new instance of your parent when you click the Navigation button. A simple approach is to just finish your activity so whatever activity in the backstack will simply be resumed.

Something like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
tompee
  • 1,408
  • 1
  • 7
  • 6