Android app restarts when user clicks on its icon [duplicate] - android

This question already has answers here:
App completely restarting when launched by icon press in launcher
(15 answers)
Closed 2 years ago.
As far as I know by default android app resumes when user clicks on its icon.
But for some reason my app (which I want to have the same behavior) restarts.
When I hold the home button and then select my app from "recent" I get it resumed.
I want the same behavior on clicking the app icon.
actually it's the same as https://groups.google.com/forum/?fromgroups=#!topic/android-developers/UjWcsFMe6ik
but they didn't find an answer
upd:
manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk a:minSdkVersion="10" a:targetSdkVersion="11"/>
<uses-permission a:name="android.permission.INTERNET"/>
<uses-permission a:name="android.permission.ACCESS_FINE_LOCATION"/>
<application a:label="#string/app_name"
a:theme="#android:style/Theme.NoTitleBar"
a:name="myapp.AppDelegate">
<activity a:name="myapp.activities.AuthorizationActivity"
a:label="#string/app_name"
a:screenOrientation="portrait"
a:alwaysRetainTaskState="true">
<intent-filter>
<action a:name="android.intent.action.MAIN"/>
<category a:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity a:name="myapp.activities.activity1"
a:screenOrientation="portrait"/>
<activity a:name="myapp.activities.activity2"
a:screenOrientation="portrait"/>
...
<activity a:name="myapp.activities.activityN"
a:screenOrientation="portrait"/>
<service a:name="myapp.location.LocationService"/>
</application>

It's all about IDE.
After closing an app which was started by IDE (eclipse or IDEA - doesn't matter) Android deletes all it's temporary data (don't ask me why)
So the solution is:
1) run app from ide (deploy it on device)
2) press back button to close an app
3) start an app again
...
and now it will resume working after quitting

Check launch mode of your activity. If its singleTask then make it standard and then check it.

Clicking the app icon will bring the task containing the main activity to the front.
To get the desired behavior make sure all your activities belong to the same task, i.e. don't use singleTask or singleInstance on any of the activities.

Related

Why is launcher activity with launchMode="singleTask" always pushed to top of backstack even if another activity was on top? [duplicate]

This question already has answers here:
Android: bug in launchMode="singleTask"? -> activity stack not preserved
(12 answers)
Closed 2 years ago.
My launcher activity has the launchMode attribute set to singleTask due to certain requirements.
<activity
android:name=".map.MapsActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="#style/MapScreenTheme"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The problem I'm facing is that if I open another activity -> press home -> Click on app icon in launcher application -> It opens the MapActivity and not the activity that was previously open.
This however does not happen if I navigate to the app via the recents menu. Then the newly opened activity stays on top.
Can someone please explain what is happening here with regards to the backstack and why is the ActivityManagerService not taking into account that the app process already exists and yet decides to start the launcher app and clear the backstack and not simply bring the app forward?
This issue can be observed in a small sample app created here -
https://github.com/abhiank/SingleTaskActivity
I think this is the desired behavior of SingleTask launch mode. Keeping Launcher Activity as SingleTask has its consequences.
I can not think of an exact solution for this problem but some workaround will do the Job.
Adding the link below which i have mentioned in comment to go through.
Android: bug in launchMode="singleTask"? -> activity stack not preserved
I think, When you click on app icon again, It opens the Launcher Activity on top of your previously opened Activity. What you can do is apply a simple check whether there are any activity in the backstack apart from this, then finish this launcher activity, It will reveal previously opened screen. Try the below link.
https://stackoverflow.com/a/38450232/3497972
check these links too, they have other ways to maintain screen when launched app icon, when app is in background
How to make an android app return to the last open activity when relaunched?
Android app restarts when opened by clicking app icon
Determine when application icon is clicked to launch the app in android
What is the difference between launch app from "recent apps" and tapping app icon
There is some solution for case when your main activity would be launched only by other activities within your own application:
You can remove launchMode attribute from your main activity definition in manifest. And just pass Intent.FLAG_ACTIVITY_NEW_TASK to every intent that opens your MainActivity, for example like this:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Be aware that behaviour of Intent.FLAG_ACTIVITY_NEW_TASK or launchMode="singleTask" within your own application only works in conjunction with taskAffinity attribute on activity definition in manifest.
I hope it will help you a little.
Sounds to me like you are seeing this nasty long-standing Android bug
Re-launch of Activity on Home button, but...only the first time
To test whether that is the case, please kill your app (settings->apps->your app->force quit). Then launch your app by tapping the app icon on the HOME screen. Do whatever you need to do to launch another Activity into the task. Press HOME. Now tap the icon on the HOME screen again. This should bring your task to the foreground in the same state you left it in.

App is getting started again after launcher click even when it is already running

I have the problem that my App is starting again if the user clicks the launcher icon even if it is already running. I used singleTaks for that Activity and as many Stackoverflow Answers said -> This should solve the problem - but it doesnt.
<activity
android:name=".activities.ActivitySplash"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
As you can see i did define the launchMode to be singleTask. I open the App. Navigate to Screen X (I even could stay on the Startscreen, has the same effect) and now i press the home button. When i now press the launcher icon again the already running app does not pop up or is getting resumed (call it as you wish). Instead the App is completly started new.
How can i prevent that behaviour, if singleTask isnt working?
Try with SingleTop:
if the target task already has an existing instance of the activity
at the top of its stack, that instance will receive the new intent (in
an onNewIntent() call); a new instance is not created. In other
circumstances — for example, if an existing instance of the
"singleTop" activity is in the target task, but not at the top of the
stack, or if it's at the top of a stack, but not in the target task —
a new instance would be created and pushed on the stack.

Fail to resume the activity stack during 1st launching after installation

Our application has a splash activity (main activity for launcher) and many other activities. In most cases, when the user switches the application into background and resumes it from launcher, the old activity stack is resumed and top activity in that stack is shown as expected.
However, when the apk is just installed on the phone, or a new apk (with higher version) of the same application is installed, its behavior is strange. When the application is switched to background and resumed from launcher, the previous activity stack is not resumed and the splash activity is always shown. If we resume the application from recent applications list, the activity stack is resumed as expected. Only after killing the task from recent applications list, everything becomes normal again. The previous activity stack will always be resumed correctly until it is replaced by another apk installation again.
My Android version is 4.1.2 and I am using its default launcher.
Following is configuration of the splash activity.
<activity android:name=".welcome.activity.SplashActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Who has any idea about the strange behavior? Thanks in advance!
Finally I have found this is an issue of Android system. It has already been discussed in Activity history stack wrong upon first install on device?.
I have adopted a workaround stated in https://code.google.com/p/android/issues/detail?id=2373#c40 and it works well, i.e. adding the following code in SplashActivity.onCreate():
if (!isTaskRoot()) {
Intent intent = getIntent();
String action = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action != null && action.equals(Intent.ACTION_MAIN)) {
finish();
return;
}
}
I'm not really able to tell what's going on without knowing more about your app. I would look into:
The launchMode manifest element: android:launchMode
Check your activities are not being destroyed and recreated by the system: Recreating an Activity
If you are doing anything with fragments and if these are maintaining state correctly
Sorry, bit of a guess!
Since you only want to show your Splash Activity once, you can use finish() after you launched the Main Activity, that should solve your problem.

Launch Home choice from an app

Hello my question is a bit difficult let me explain and try to understood
I have a custom Home who is set as default when user press home. Know i need a way to ask wich home use when i press on a setting key, or a way to launch Setting activity for delete home default action.
In fact i wan't a way to go back to home app installed by google instead of mine just in case.
I need this because my custom home is still in dev and don't have all feature yet.
I need to keep my home as a default action when user press home buton but i need to add a home selector in special case.
I have try to use finish() when i need to go back to old home but mine as default is automaticly relaunch because he is the default one.
Any idea?
EDIT
I try to reformul a bit.
I'm making a home apps. I set it as default on my tablet. My home apps don't have all feature yet. And when i will launch something or go to setting i need to relaunch default home app.
So i need a way inside my app to reset default home action or launch a home apps, or ask user to choice wich app he will use has home apps.
Hope this is clear for all.
Probably you should use this one to implement your idea. keep the below lines in your manifest file.
<activity android:name="Home"
android:theme="#style/Theme"
android:launchMode="singleInstance"
android:stateNotNeeded="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
For more info about the sample project. just go the below path in your Android SDK
\android-sdk\samples\android-8\Home
EDIT :
Hi Please check this to pro grammatically unset the default application.
How do I use PackageManager.addPreferredActivity()?
for more info go through this also to clear the clearPackagePreferredActivities
http://developer.android.com/reference/android/content/pm/PackageManager.html#clearPackagePreferredActivities%28java.lang.String%29
hope this helps you.

Overriding Home button for a Car Home replacement app

I have been working on a replacement for the stock Car Home app for a bit, and I am completely stumped on how to override the Home button so that my app will be brought back to the foreground whenever the phone is docked. This is the way that Car Home works, so there must be a way.
It seems that BroadcastReceivers won't work, because the intent that is broadcast whenever the Home button is pressed will still cause the default homescreen app to launch; I cannot prevent it. I can override the Home button from within my app, but that does me no good since this needs to work when the user is outside my app. Car Home also does not do anything weird like set itself as the default homescreen app while it's running (I checked the logcat to make sure).
What can I try next?
Well, after many months I have finally found the answer to this question. The key is the "android.dock_home" metadata element, found here:
http://developer.android.com/reference/android/content/Intent.html#METADATA_DOCK_HOME
By using this in your AndroidManifest.xml, you can make your dock application become the home application temporarily. To do this, add this line to the AndroidManifest.xml inside the Activity tags for the dock app activity:
<meta-data android:name="android.dock_home" android:value="true" />
If the value is set to true, as long as your phone is docked the Home button will return you to the dock app. After undocking, the Home button will take you back to your normal home app.
Unfortunately, there is no way in the public APIs to override the Home button without the user confirming it.
Your best bet would be to implement a CATEGORY_HOME Intent. This means when a user pressed Home they would be presented with the option to run the standard Home or yours and make yours the default if they wanted.
When your application was launched you could then check if the phone was docked. If the phone is not docked you could then open the standard Home screen and close your app before anything is displayed.
You need to the correct intent filter in your manifest for the app to launch automatically when you dock the phone. Refer to http://developer.android.com/reference/android/content/Intent.html#CATEGORY_CAR_DOCK for the information.
I found a way to tackle HOME key. For your application set the manifest as
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
Now your application is an alternate Launcher application.
Use the adb, and disable the launcher application using package manager
pm disable com.android.launcher2.
Now the Home key press will aways stay in the same screen.

Categories

Resources