0

In my application I have three activities:

    <activity
        android:name="com.example.myapp.SplashScreenActivity"
        android:exported="true"
        android:launchMode="singleInstance"
        android:noHistory="true"
        android:screenOrientation="sensorLandscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myapp.MainActivity"
        android:exported="false"
        android:launchMode="singleInstance"
        android:screenOrientation="sensorLandscape" >
    </activity>
    <activity
        android:name="com.example.myapp.ListActivity"
        android:exported="false"
        android:launchMode="singleTop"
        android:screenOrientation="sensorLandscape" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myapp.MainActivity" />
    </activity>

The first one is the LAUNCHER, SplashScreenActivity, which is a splash screen that disappears quite soon and it's not shown in recent activities, it starts MainActivity. In MainActivity users can select a category and ListActivity shows the items belonging to the given category. This is done with the following code:

Intent i = new Intent(getActivity(),ListActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.putExtra("category",mCategory);
startActivity(i);

In ListActivity the onResume method checks for the "category" extra and shows data accordingly. Since Activity launchMode is singleTop, I've also overridden the onNewIntent method to set the new Intent of the Activity.

This works properly if the app doesn't go in background: in this case, when I restart MainActivity and select a category, ListActivity resumes the old Activity showing data belonging to the previously chosen category.

How should I fix flags/launchMode in such way that my app doesn't resume ListActivity with the old data loaded?

Vektor88
  • 4,841
  • 11
  • 59
  • 111
  • Does it happen when you have quit your app by pressing back button, and then resume it from the list of recent apps? Or does it happens always? – B.B. Dec 29 '16 at 13:24

2 Answers2

1

You should not use the special launch modes. These are rarely required and only in very specific circumstances. Remove all the launchMode specifiers from your manifest. You also don't need to use these flags when launching ListActivity from MainActivity:

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

Your use of singleInstance launch mode is causing you all these problems.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • With default launch modes, if a user keeps pressing back he'll see the old `ListActivity` and `MainActivity` stack. – Vektor88 Dec 29 '14 at 13:31
  • Then you aren't doing it right. When are you launching a `ListActivity`? Explain more of your architecture please. – David Wasser Dec 29 '14 at 14:27
  • You should only ever have 1 `MainActivity` and 1 `ListActivity`, but you don't need special launch modes to achieve this. – David Wasser Dec 29 '14 at 14:28
  • Let's say that my app works like a launcher, even if it's not. `MainActivity` contains categories, `ListActivity` contains lists of applications and is able to execute them. If I run an application inside `ListActivity` and my app goes in background. When I restart it, choosing a category in `MainActivity` resumes the `ListActivity` with the previous category set instead of loading the new list of apps. It's important that if user keeps pressing back button doesn't see stacked activities. – Vektor88 Dec 29 '14 at 14:41
  • When you "restart it", it should bring the background task to the front, and if you had `ListActivity` on top, then it will still be on top. So there is no way that you can go to `MainActivity` and start another `ListActivity`. Don't stack the Activities in the first place and you won't have this problem. – David Wasser Dec 29 '14 at 14:52
  • Unless you really have a launcher app, which is a special case. – David Wasser Dec 29 '14 at 14:53
  • What I mean is that if user launches the app again by clicking the icon of my app on the launcher, it goes to `MainActivity` and here instead of creating a new `ListActivity` with the new category, it starts the previously created one. Of course resuming the app from recent applications brings me to `ListActivity` as expected. – Vektor88 Dec 29 '14 at 15:11
  • But that's wrong. If your app is in the background, with `ListActivity` on top and the user launches the app again by clicking on the icon of your app in the launcher, it should just bring the existing task to the foreground, with `ListActivity` on top. That is standard Android behaviour. If you are not seeing that, then there is something else broken. Have you started your app directly from the app installer or an IDE? If so, you could be seeing this nasty bug: http://stackoverflow.com/a/16447508/769265 – David Wasser Dec 29 '14 at 15:43
0

Assuming you would ditch all the launchModes and Flags, then activity lifecycle gives you all the information needed to implement application as you have described - maybe there is somethings you have not added?

Once application goes to background, then ListActivity will be allowed to save its state, even if system will kill your process (lack of memory etc.), then still - launching back your app will first bring back ListActivity with previously saved instance state. So you will not have behaviour as you describe : when I restart MainActivity and select a category, ListActivity resumes the old Activity showing data belonging to the previously chosen category.

Still, if you need to keep to your current design, then you should somehow inform ListActivity of new data changes, question is if ListActivity.onNewIntent is being called at all in the case your describe? Have you tried adding Intent.FLAG_ACTIVITY_SINGLE_TOP : as per this blog entry: http://www.acnenomor.com/1094151p2/bug-onnewintent-not-called-for-singletop-activity-with-intentflagactivitynewtask ?

marcinj
  • 48,511
  • 9
  • 79
  • 100