Re-launch of Activity on Home button, but...only the first time

The application that I am developing has a weird behavior the first time that it is installed. If the user backs out of the application normally the first time, it will forever behave as it should. If the user uses the home button the first time after installing the application, it treats the application as though it should re-launch the home screen again and launch a new version of an activity in front of the old one.

So there are really two issues at hand. I can't seem to resolve both of them.

  1. Keep the application from closing when it is first installed upon the user hitting the home button.
  2. Keep multiple versions of the application from launching when it does this (launchMode helps here a bit, but the first component still triggers).

I don't have the launchMode attribute in the manifest file defined as anything. So there shouldn't be any odd behavior as the result of this. I have experimented now with the launchmode attribute of the application to see if I could get it to behave the way it is intended, but there seems to be more to it here than just launching the activities properly. There is no reason that an application should close itself the first time as far as I can see when the home button is pressed.

I don't use the onUserLeaveHint within the application either. I had to be sure once again by doing a search over the project. So there doesn't seem to be any overriding of the home button attempted at all.

Even after re-starting the phone, the home button behaves normally again. Not sure what causes the initial installation to treat the home button as a flag to start the application from scratch.

Once the user backs out of the application the first time, the issue is permanently resolved. Any thoughts on where I should look?

Recently there has been a search within the application to see if perhaps it triggers only then because of an onUpgrade() component of the SQLite database method causing some odd behavior.

@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
                if (newVersion > oldVersion) {

           }
       }

Or in another location that might trigger with an update from the manifest file if I was passing along a newer version of the APK along to a device that had a version on it already that was one lower than the current one. However, nothing in this part of the code leads me to believe that it should affect anything related to the launch sequence.

The manifest file (with names changed) is supplied below for what is currently being used in the application.

<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="com.android.vending.CHECK_LICENSE"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<uses-permission android:name="android.permission.VIBRATE" android:required="false"/>
<uses-permission android:name="android.permission.CAMERA" android:required="false"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>

<application android:icon="@drawable/icon"
             android:label="@string/app_name"
             android:allowBackup="false"
             android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
             >
    <activity android:name=".MyMain"
              android:label="@string/app_name_with_version"
              android:screenOrientation="portrait"
              android:windowSoftInputMode="adjustPan"   />
    <activity android:name=".StartupActivity"
              android:label="@string/app_name"
              android:screenOrientation="portrait"
              >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity   android:name=".SelectActivity"  
                android:label="@string/app_name_with_version"
                android:screenOrientation="portrait"/>
    <activity   android:name=".ImageSelectionActivity"
                android:theme="@android:style/Theme.Dialog"
                android:label="@string/app_name_with_version"
                android:screenOrientation="portrait"
                android:configChanges="orientation|keyboardHidden"/>            
    <activity   android:name=".DetailsActivity"
                android:label="@string/app_name_with_version"
                android:screenOrientation="portrait"/>
    <activity   android:name=".EMailActivity"   
                android:label="@string/app_name_with_version"       
                android:screenOrientation="portrait"/>
    <activity   android:name=".SendTo" 
                android:label="@string/share_label" 
                android:theme="@android:style/Theme.Dialog" >
        <INTENT-FILTER>  
            <ACTION android:name="android.intent.action.MAIN">  
            <CATEGORY android:name="android.intent.category.LAUNCHER"/>  
            <INTENT-FILTER>  
                <ACTION android:name="android.intent.action.VIEW">  
                <CATEGORY android:name="android.intent.category.DEFAULT">  
                    <CATEGORY android:name="android.intent.category.BROWSABLE">  
                    <DATA android:scheme="callback" android:host="twitter"/>  
                    </CATEGORY>  
                </CATEGORY>
                </ACTION>
            </INTENT-FILTER>
            </ACTION>
        </INTENT-FILTER>
    </activity>
    <activity   android:name=".CreateMyActivity"
                android:label="@string/create_account_label"
                android:screenOrientation="portrait"
                android:theme="@android:style/Theme.Dialog"/>
    <activity   android:name=".ViewInLayoutActivity"
                android:label="@string/app_name_with_version"   
                android:screenOrientation="portrait"/>
    <activity   android:name=".Preferences"
                android:label="@string/set_preferences" 
                android:screenOrientation="portrait"
                android:theme="@android:style/Theme.Dialog"/>
    <activity   android:name=".AboutActivity"   
                android:label="@string/app_name_with_version"       
                android:screenOrientation="portrait"/>
    <activity   android:name=".InteractiveActivity" 
                android:label="@string/app_name_with_version"
                android:screenOrientation="portrait"
                android:theme="@android:style/Theme.Dialog"/>   
    <activity   android:name=".AlertFailedCommunications"
                android:screenOrientation="portrait"
                android:label="@string/alert_account_label"
                android:theme="@android:style/Theme.Dialog"/>
</application>    


Welcome to the ever-growing list of users who have been bitten by this one.

This is a well-known and long-standing Android bug. in the way applications get launched the first time from the installer, web-browser and via IDE (IntelliJ, Eclipse, etc.). See these issues filed long ago related to the problem:

http://code.google.com/p/android/issues/detail?id=2373

http://code.google.com/p/android/issues/detail?id=26658

It is still broken and you cannot prevent this from happening. The only thing you can do is to detect when Android has launched a second instance of your root activity into an existing task. You can do this by putting this code in onCreate() of your root activity:

if (!isTaskRoot()) {
    // Android launched another instance of the root activity into an existing task
    //  so just quietly finish and go away, dropping the user back into the activity
    //  at the top of the stack (ie: the last state of this task)
    finish();
    return;
}

Why this is behaving, no clue. But I know that custom Launcher have specific launchModes set.

For example the ADW.Launcher:

android:clearTaskOnLaunch="true"
android:launchMode="singleTask" 

Home application in the Android SDK samples: android-sdk\samples\android-16\Home\AndroidManifest.xml

android:launchMode="singleInstance"

To quote CommonsWare: How to prevent custom home launcher app restart activity?

I am going by the Home sample application from the SDK, which uses singleInstance. Curiously, the AOSP launchers use singleTask


Looks like the issue is caused by the different instances of the app. Probably the first instance is launched under the installer's task and the second one is under its own task, when user clicks on app icon. It seems to user he/she resumes the app but actually he launches it again at this moment.

If you want to get your app to run in its own task only, a possible way is to add

android:allowTaskReparenting="true"

to the application-level of your AndroidManifest. Once the app icon is clicked for the first time after installation, your application will be moved to the separated task.