I'm experiencing a strange issue on a new app I'm currently developing, where if I send the app to background (Home button), and then relaunch the app using the App Icon, all activities, except for the root activity are destroyed (on log I get OnDestroy for all activities, except for the root activity where OnResume is called).
If I send it to background and resume it from the current tasks, it resumes the app.
I have added android:launchMode="singleTask" to all the activities on the app manifest but it made no difference.
Every activity is started normally: startActivity(new Intent(this, nextClass));
Is there any way to resume the app when pressing the App Icon instead?
AndroidManifest.xml
<application
tools:replace="android:icon"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.example.game.classes.App">
<activity
android:name=".LaunchScreenActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:launchMode="singleTask"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:launchMode="singleTask"
android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity
android:name=".PlayActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:launchMode="singleTask"
android:configChanges="keyboardHidden|orientation|screenSize" />
.
.
.
EDIT: Explanation to as not duplicate of App restarts rather than resumes
The difference between that situation and mine is that, on that case the main activity is launched on top of the other activities (you can press back to return to previous running activities). In my case the other activities are completely destroyed except for the launch activity which is resumed.
After days searching for a solution.. I finally found the problem.
The issue was adding android:launchMode="singleTask" to every activity.
I removed it from all activities except from the root activity and now it is working as I wanted.
Related
I have the following manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="#style/AppTheme"
<activity
android:name=".LoginActivity" android:launchMode="singleTask" 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=".MainActivity" android:screenOrientation="portrait"/>
<activity android:name=".TaskDetailActivity" android:screenOrientation="portrait"/>
<activity android:name=".ConfigurationActivity" android:screenOrientation="portrait"/>
</application>
I don't know why, if I minimize the application and then i click on application icon, the app get relaunched instead of going to the last activity.
If I use application switcher instead of application icon for opening the application, It goes back to the last activity on use, so it works as expected.
Why is this behavior? I want to open the last activity when I click on application icon not restart the whole application.
An Activity with singleTask launchMode is allowed to have only one instance in the system.
Remove android:launchMode="singleTask" for more about singletask
After testing my application on multiple device i found out that on some of them if i click application icon it shows me
WelcomeActivity instead of bringing back the application from background. I know that WelcomeActivity is LAUNCHER but on my other devices the app icon alway bring me back the background application.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:launchMode="singleTop"
android:noHistory="true"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".WelcomeActivity"
android:screenOrientation="portrait"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="#style/AppTheme"
android:windowSoftInputMode="adjustPan">
After WelcomeActivity i use MainActivity. and usually the app is putted to the background while that was alive.
Remove
android:launchMode="singleTop"
Because
If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.
Source
I'm sure that this will be a really newbie question but I'm stuck and I don't know how to get out of this!
I have an app that have three activities and when I use HOME button and I open the app again, it goes to the FIRST activity always, even if I was in the second or at the third one.
EDIT 3: My app comes in three activities, the first one is the main menu, the second is a map of tables and the third one are the data of the tables. Depending of the configuration, closing the third activity must bring me to the first one or the second one, and when I'm leaving the third Activity I dont want it to stay on the Activities stack. My program is working fine going from an Activity to another one. My problem is that seems that when I use Home Button my app finishes every Activity except the first one.
Maybe I have to modify anything on the manifest or maybe I have to use in a specifically way the RestoreInstanceState but I'm searching so hard and I can't find anything. Thanks in advance!
Edit 1: I'm adding my 'application' xml part of the Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/icobaccus"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.tpv2_tablet.Activity_Start"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard"
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:name="com.example.tpv2_tablet.Activity_Zonas"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<activity
android:name="com.example.tpv2_tablet.PrintDialogActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<activity
android:name="com.example.tpv2_tablet.Activity_Mesas"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard">
</activity>
</application>
Edit 2: Maybe I'm doing something wrong when calling other activities or I'm calling a wrong flag:
Intent intent = new Intent(Activity_1.this, Activity_2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
Remove
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
from all activities except launcher activity
To understand the reason why, read Google Documentation here
remove:
android:noHistory="true"
As this will remove the activity from the activity stack
remove:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
from:
Intent intent = new Intent(Activity_1.this, Activity_2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
to understand the reason check this answer
You need to set android:clearTaskOnlaunch="false" in your android manifest.
I have 3 activity tasks A-B-C
<activity android:name=".LoginActivity"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DashboardActivity"
android:label="#string/app_name" >
</activity>
<activity android:name=".CreateNewPolygonActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:configChanges="orientation|keyboard|keyboardHidden">
</activity>
<service android:name=".services.RecordCircuitService"
android:enabled="true" >
</service>
And I have a Service with GPS using LocationManager.
My application has this logic:
On launcher icon clicked - > start login activity
After login - > login activity is finish(); and start dashboardActivity
In the dashboard I start CreateNewPolygonActivity and start the Service with GPS
Press HOME (this is reorganize RecordActivity to foreground)
after I have some mistakes work
open CreateNewPolygonActivity with onCreate
open DashboardActivity
I think I have a problem with my task organization.
You won't achieve the desired result by setting launchMode="singleTask" for your activity. Android won't create a separate task for this because you've not set the taskAffinity. In any case, you don't want to do this by using launchMode="singleTask" because this causes more problems than it solves. This launch mode is intended for HOME-screen replacements only!
Remove launchMode="singleTask"`` fromCreateNewPolygonActivity`
Also, you should remove android:noHistory="true" from your login activity. This isn't necessary either. Since you've already finished the login activity when the user starts the DashboardActivity, when he presses the BACK button, it won't return to the login activity (which I assume is what you wanted).
Once you've done these things, please tell us what is still broken.
I have 2 activities in my app. The A activity is the launcher one. When I run the app the very first time, the launcher activity runs, but when I press the home button and restart the app from there by clicking the app icon, I always get the B activity running.
I want to make sure that the activity A should always run when starting the app.
This is the manifest code:
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name="com.velosys.smsManager.Activities.a"
android:clearTaskOnLaunch="true"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.velosys.smsManager.Activities.b" />
</application>
I have searched a lot for this but did not get any clue.
Maybe android:launchMode="singleInstance" on activity B will get it done?