Search code examples
androidandroid-activityrestart

Android - Wrong activity sometimes starts


I have an Android App with a number of activities. The wrong activity is being started sometimes.

Normally, an Application subclass starts, then start activity (StartAct... android:name="android.intent.action.MAIN", android:name="android.intent.category.LAUNCHER") does some work and then launches InitializeActivity. This does some work and then fires off my main display activity (MainAct). The first two activities do some essential initialization including setting a static "isInitialized" flag just before the intent is launched for the MainAct.

Activities are launched with startActivity() using a specific intent (...activity.class specified), and call finish() after startActivity().

However, here is what sometimes happen, and I don't know why...

In short, the app is killed and when the icon is pressed to start it, it jumps straight to the third (MainAct) activity. This causes the app to detect an error (isInitialized flag is false) and stop:

  • Launch the app normally with the Icon:
  • ...Application subclass starts, also fires up some worker threads
  • ...StartActivity runs, then fires InitializeActivity and finishes
  • ...InitializeActivity runs, then sets isInitialized and starts MainAct and finishes
  • ...MainAct starts, runs okay
  • ...Home button is hit and Angry Birds is run
  • ...MainAct logs onPause, then onStop.
  • ...Worker threads owned by Application subclass continue to periodically do stuff and log.
  • After 25 minutes, the entire application is suddenly killed. This observation is based on thhe end of logging activity,
  • Time goes by
  • Home button hit
  • Launcher ICON is pressed for the app
  • Application subclass onCreate is called and returns
  • *MainAct.onCreate is called! (no StartAct, no InitializeActivity)*

What am I missing?

Note: the initialize flag was added because of this issue. It is set in the only place in the code that starts the main activity, and checked only in onCreate in the main activity.

[per request] Manifest file (slightly redacted). Note that the service in here is not currently used.

<manifest
  xmlns:android="http://schemas.android.com/apk/res/android"
  package="xxx.yyy.zzz"
  android:versionCode="1" android:versionName="1.0.1">
  <application
    android:icon="@drawable/icon_nondistr"
    android:label="@string/app_name"
    android:name=".app.MainApp"
    android:debuggable="true">
    <activity
      android:label="@string/app_name"
      android:name=".app.StartAct" android:theme="@android:style/Theme.NoTitleBar">
      <intent-filter>
        <action
          android:name="android.intent.action.MAIN" />
        <category
          android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity
      android:label="Html"
      android:name=".app.HtmlDisplayAct"/>
    <activity
      android:label="Init"
      android:configChanges="orientation"
      android:name=".app.InitializeActivity" android:theme="@android:style/Theme.NoTitleBar"/>
    <activity
      android:label="MyPrefs"
      android:name=".app.PrefsAct" />
    <activity
      android:label="@string/app_name"
      android:theme="@android:style/Theme.NoTitleBar"
      android:name=".app.MainAct">
    </activity>
    <service
      android:name=".app.svcs.DataGetterService" />
  </application>
  <uses-sdk android:minSdkVersion="4"/>
  <uses-permission
    android:name="android.permission.INTERNET" />
  <uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission
    android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission
    android:name="com.android.vending.CHECK_LICENSE" />
  <uses-feature
    android:name="android.hardware.location.network"
    android:required="false" />
</manifest>

Solution

  • The fact that the application is killed because of low memory should be transparent to the users. This is why when the application is killed, Android remembers what was the last activity running in this application, and creates directly this activity when the user returns to the application.

    Perhaps you could do something in the onCreate() method of your Application (or of your MainAct) to ensure that everything is properly initialized.

    By the way, unless you really need to, you shouldn’t have worker threads doing some work when the user is not using your application. Depending on what you do this could drain the battery quickly, or make the user think that it could drain the battery quickly (which is worse, because the user will uninstall your app!)

    You could also make the application finish every activity when the user is quitting the application,