I am developing app, which works as kind of launching hub for other applications. Thing is, my application is always destroyed by system after about 75 seconds after any other external activity is launched from it and stays active.
For instance I am launching browser, or any activity with chooser in it. Then waiting for 75 seconds and back button will take me to Home. With chooser activity I even may see my application exits in background.
How do I avoid this, what may be the reason? My guess is I should have some sort of affinity with launched activities, but I may be wrong.
Here is how I describe my activities. The app is called Speaktoit Assistant on the market you can test it.
<activity
android:name="com.speaktoit.assistant.main.MainActivity"
android:theme="#style/Theme.DoNotDim"
android:windowSoftInputMode="stateHidden|adjustPan"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTop">
</activity>
<activity
android:name="com.speaktoit.assistant.SplashActivity"
android:label="Assistant"
android:theme="#android:style/Theme.Light.NoTitleBar"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation"
android:finishOnTaskLaunch="true"
android:noHistory="true"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
UPDATE:
Ok, Looks like system halts my app and returns it to initial launcher activity and since SplashActivity is finishOnTaskLaunch="true", then it just exists. Question is how make android to return to MainActivity instead ...
UPDATE 2:
Ok, I have found killTask buried in the code. Problem solved :).
Did you try removing singletop launchmode?
Related
Can we destroy instance of an app in the background after opening it from a deep link? Like you need to login first in that app in your phone before you can proceed in the browser. After successful login, is it possible to destroy the activity which is now in the background (in this case the screen is now the browser)?
I was able to finish the activity and in that case it navigates me back to the browser. But when I try to open the RECENT APPS BUTTON it still shows me the application on background.
Thank you for your help.
In AndroidManifest.xml, add android:excludeFromRecents="true" to MainActivity of your app like below:
<activity android:name=".MainActivity"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
It should work. For details please check here.
Note: If above doesn't work then please try by adding android:launchMode="singleInstance" as well.
I Have the Code in manifest:
<activity android:name=".activities.login.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.main.screen.MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" />
What is happening:
The i load the app for the first time (Ex: Install it from
androidStudio) 'MainActivity' is loaded but once i close and launch
again LoginActivity is launched everytime
This happens for the first time
MainActivity launching happening for the first time
Its should always load 'LoginActivity' right since my launcher is
'LoginActivity' why is LoginActivity loaded just for first time. Is
there any setting i need to check
You have used
android:launchMode="singleTop"
in your main activity. According to docs -
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.
So that's why MainActivity is loading.
You should remove
android:launchMode="singleTop"
to get desired results.
For more information on launch modes refer docs -
https://developer.android.com/guide/topics/manifest/activity-element
I need the following problem to be resolved:
From the UnityPlayerNativeActivity I am starting a standard Android activity (may be mine, may be with an ad from the ad network - nevermind). When the game is being hidden with this activity on top (not the Unity one) by pressing the home button, as a user I have two ways of restoring it:
from the Recently used apps screen - the app is being restored to the same state, when it was minimized (that is what I expect to happen);
from the launcher, what causes the game's UnityPlayerNativeActivity is being restored with losing all other activities, that have been opened on top of it.
These activities are lost somehow (in a way I don't exactly know, what has happened with them). My game's logic depends on the result of the processes happening there, ie. I need to know, that this particular activity has been exited in any specific way (give a callback for example).
Do you know how I can bring this Unity activity back from launcher with all activities above it, as it was while minimizing it?
I want to understand the difference between the ways of restoring the app from Recently used screen and the launcher. I guess it is related to the intent-filter section within AndroidManifest.xml file, which is included within UnityPlayerNativeActivity entry.
The solution turned out to be quite simple, but not so obvious.
It has turned out, that the AndroidManifest.xml configuration that is being produced by Unity by default is causing this problem. For the main activity that is being started from launcher the following parameters are defined (taken from decompiled app):
<activity
android:alwaysRetainTaskState="true"
android:clearTaskOnLaunch="true"
android:configChanges="locale|fontScale|keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"
android:label="#string/app_name"
android:launchMode="singleTask"
android:name="com.unity3d.player.UnityPlayerNativeActivity"
android:screenOrientation="fullSensor"
launchMode="singleTask">
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
</intent-filter>
</activity>
The problematic behaviour is being caused by these 2 parameters:
android:clearTaskOnLaunch="true"
android:launchMode="singleTask"
When the I change them to:
android:clearTaskOnLaunch="false"
android:launchMode="standard"
Then the app is resuming fine from the launcher.
The correct parameters should be set as follows:
<activity
android:alwaysRetainTaskState="true"
android:clearTaskOnLaunch="false"
android:configChanges="locale|fontScale|keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"
android:label="#string/app_name"
android:launchMode="standard"
android:name="com.unity3d.player.UnityPlayerNativeActivity"
android:screenOrientation="fullSensor"
launchMode="standard">
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
</intent-filter>
</activity>
Note: While I am able to set the value android:clearTaskOnLaunch="false" for this activity explicitly, the launchMode parameter will be forced by Unity3d and set to "singleTask". I managed to change and check it by decompiling the app and rebuilding from these changed resources. I wonder if there is any elegant way to set this value.
You may find this blog post useful:
http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
I made myself an app: a music player with automatic bookmarking :)
Problem:
If I go "back" to the home page and then try to run the app again, it creates a new instance whereas I want to continue. I might leave music running while I do other things and then I can't pause it or save it because I can't access it!
Failed Solutions:
I've tried singleTop, singleTask, singleInstance and they all don't work!
I can view running apps (Settings > Applications > Manage Applications > Running) but it only let's you "Force stop" them. How about "Bring to front"?! Also, when I finish() my app, I notice it's still "running"!
This site is very awesome and useful and this is my first question :) I looked for answers but couldn't find any that worked :(
Here's the manifest:
<application android:icon="#drawable/icon"
android:label="#string/app_name">
<activity android:name="Fire"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Back by default closes the activity. You can override this behaviour by catching BACK key, but in your case, you really need Service not activity to play music and control it. Activity is ment to be destroyed when you leave it on BACK really....
Read more here: http://developer.android.com/guide/topics/fundamentals/services.html
Hello
My application works like this.
StartUpActivity is called first, which does a lot of the init stuff
Then it launches TvbTabActivity (TabActivity) that has other Activities as its tabs (e.g. BrowseActivity).
The problem that I am seeing is this - when a task-killer app is used to terminate my app on TvbTabActivity/Browse tab, and the app is relaunched again, the system forgoes the normal flow (StartUpActivity is not spawned), but instead restores the last visible activity directly (TvbTabActivity).
How can i force Android to ALWAYS run StartUpActivity first, so that it initializes the app?
Obviously, I dont have this problem when my app crashes on its own, lol, due to an exception, and is then relaunched again.
<application android:icon="#drawable/appicon"
android:label="#string/app_name" android:name="com.xyz.QPApplication"
android:debuggable="true">
<activity android:name=".activity.StartUpActivity" android:configChanges="locale|orientation"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".catalogue.BrowseActivity" android:configChanges="locale|orientation"
android:label="#string/app_name" android:screenOrientation="portrait"
android:launchMode="singleTop">
<intent-filter>
<action android:name="com.xyz.android.intent.action.BROWSE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".activity.TvbTabActivity" android:configChanges="locale|orientation"
android:screenOrientation="portrait" android:launchMode="singleTask">
</activity>
You can't. Android will try to restore the app where it left off. The correct way to handle this is to ensure that you understand the Activity life-cycle and put the appropriate initialization in the appropriate place.
There are a couple of ways to solve your issue, the best would be to check the Android Life-cycle diagram http://code.google.com/android/images/activity_lifecycle.png and try to figure out a way to make the app work within that context.
Of course if you really want to you can kill your own app by calling Activity.finish() when it hits the onPause() or onStop() states, but that is quite an ugly solution.
You can't do anything about this -- what is happening to you is what the force stop API does and is intended to do.
Task killers are abusing that API.
They can no longer use it in 2.2 and later.
If you really want to avoid it, you could limit your app to only 2.2 or later. Or if the problem is users are complaining about them, tell them to stop using task killers. Or if the problem is just that you don't like this happening when you use a task killer, then don't use a task killer.
Also this is the same behavior that happens when the user presses "Force stop" in the manage application's UI. That is generally fine though since the user must explicitly do that, instead of what these task killer apps have been increasingly doing where they just whack stuff in the background without the user being directly involved.