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:
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>
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,