0

I have many "internal" activities in my Android application that I only want to be started from inside my application by code I've written. These "internal" activities have no intent-filter tag in the Android manifest file. I have one activity, named SplashActivity, that I use as a splash screen that has the typical launch intent-filter:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

I was hoping/expecting that whenever Android launched my app and created my custom Application object, it would always start my SplashActivity. However, some of my users have encountered launches where one of my "internal" activities which have no intent-filter are started. I believe that activity was typically the last activity used in a previous invocation of the app. I have not been able to reproduce the issue myself. However, is there some scenario where Android will launch my app, creating my custom Application object, but starting one of my internal activities that has no intent-filter. Under what circumstances will Android do so?

To easily reproduce the scenario where Android application launch starts an activity that has no intent-filter, first open your application to any such activity. Press the Home button. Then using Android Device Monitor (DDMS) to find the process that is running your application and stop/kill that process. Then launch your app. Android will create your Application object but will start/restore the activity that was last displayed instead of the starting the one with the MAIN LAUNCHER intent-filter.

KGBird
  • 789
  • 10
  • 9

3 Answers3

4

Android can kill the OS process hosting your app at any time. Usually this happens when your app has been in the background for a while (ie: the user navigated away from your app to go do something else). This happens all the time.

When the user then returns to your app, Android creates a new OS process for the app, and creates a new instance of the Activity that was on the top of the stack (ie: the Activity that was showing on screen before the app was pushed to the background).

If you don't want this to happen, you can add the following attribute to the <activity> declaration for SplashActivity:

android:clearTaskOnLaunch="true"

This will force Android to always restart your app from the beginning if your user returns to it. However, this might make your users complain, because if the user is using your app, then takes a phone call, then returns to your app, it will start from the beginning again.

It is better if you detect the problem yourself, and redirect to the SplashActivity only when necessary (ie: when your app needs to be initialized because the process has been killed and restarted). To do this, declare a static variable named initialized in SplashActivity that you set to true when your SplashActivity has successfully initialized the app. In every other Activity, do this in onCreate():

super.onCreate(savedInstanceState);
if (!SplashActivity.initialized) {
    // Android killed my process, need to redirect the user to `SplashActivity`
    redirectIntent = new Intent(this, SplashActivity.class);
    startActivity(redirectIntent);
    finish();
    return;
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I had to move the call to super.onCreate() into the if block and pass null as the savedInstanceState because super.onCreate() restored various fragments before everything is initialized. – KGBird Apr 18 '17 at 19:11
  • Sure, OK. As long as you call through to `super.onCreate()` before the `return` statement (otherwise it will crash). – David Wasser Apr 19 '17 at 08:10
0

This happens when the application is still "alive".

When you exit an application, Android does not kill it. It will remain there until the memory is needed. In this case, when the use re-launches it he will get the last Activity.

theblitz
  • 6,683
  • 16
  • 60
  • 114
  • I would love to reproduce the issue. How do you "exit" the app but leave it in this state? Back/Up does finish() in my app but when I launch the app again I see my SplashActivity. Clearly some of my users don't and I'm trying to figure out how can I put my app in this state so that I can test and make sure my app works in this scenario. I've trie Back, Home, the recently used apps. I'm just not sure how to put my app in this state. How can I put the app into this state for testing and development purposes? – KGBird Apr 18 '17 at 06:50
  • To the best of my knowledge my custom Application object which contains some necessary state is being destroyed and created again. Would Android recreate my custom Application object but not launch my launch Activity? Perhaps that's just the way Android works but that seems wrong kind of wrong to me. – KGBird Apr 18 '17 at 06:51
  • Is there any way to prevent Android for starting that last activity and instead forcing it to open the activity specified by the main launcher intent-filter? – KGBird Apr 18 '17 at 06:52
0

Can you try adding

android:clearTaskOnLaunch

for the root activity in your manifest file.

Aldrin Joe Mathew
  • 472
  • 1
  • 4
  • 13