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

Android

Android Problem Overview


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.

http://schemas.android.com/apk/res/android" package="com.mypackage" android:versionCode="6" android:versionName="2.0.0" android:installLocation="preferExternal">

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

Android Solutions


Solution 1 - Android

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;
}

Solution 2 - Android

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: https://stackoverflow.com/questions/4548947/how-to-prevent-custom-home-launcher-app-restart-activity#comment4987787_4549337

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

Solution 3 - Android

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.

Solution 4 - Android

The answers above didn't work for me using Nativescript - this solved the issue by adding these two properties in the activity tag:

android:alwaysRetainTaskState="true"
android:launchMode="singleTask"

This link provided this solution: https://github.com/NativeScript/NativeScript/issues/3415

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJay SnayderView Question on Stackoverflow
Solution 1 - AndroidDavid WasserView Answer on Stackoverflow
Solution 2 - AndroidRvdKView Answer on Stackoverflow
Solution 3 - AndroidRuslan I.View Answer on Stackoverflow
Solution 4 - AndroidSteven PfeiferView Answer on Stackoverflow