I have an activity that performs a small bit of code and then calls finish. This works fine and the activity goes away, but if I press the history button (if it's called the history button?) the activity still appears to be there, though clicking it will do nothing.
I have enabled android:noHistory="true" in my manifest. I only have a single activity that changes some settings, and then calls finish, nothing else.
Is there a way for me to make that trace of the activity go away?
Assuming that the history button you refer to is the list of recent apps, you can set
android:excludeFromRecents="true"
By default value for this attribute is false.
For other attributes check the link: http://developer.android.com/guide/topics/manifest/activity-element.html
Related
It is important in my app that when a user presses the home button and the app enters the "Background" the app reverts to the login screen next time it is opened.
I have tried using the
android:noHistory
in the manifest but this means when pressing back on activities it reverts also to the login screen so is no good. I have also tried using
android:clearTaskOnLaunch="true"
in my initial activity but this doesn't seem to have any effect. I don't finish() the first activity so i am not sure why this doesn't work.
Can anyone help with a way of doing this.
I got this working by using
android:clearTaskOnLaunch="true"
Turns out that I didn't have it in my root activity, this was because I have a second login screen which asks for less details depending on if you have logged in before.
Although it works exactly as i want it to, apparently using this "Android" feature is incorrect because i am going "against" android by clearing the tasks (activities) when the home button is pressed and my app should follow someone else opinion of what an "app" should do. Who Knew!
Why don't you use the onPause()-Method? This should be called every time the Activity gets in Background.
So something like:
#Override
public void onPause(){
super.onPause();
finish();
}
Due to the fact that only one Activity of your App can be active at a time you don't have to worry about others still being active.
Say someone is using my app, and they get to a critical state, ex they are entering some data. Say at that same time the phone rings so they answer the phone, forcing my app into the background. After the phone call ends, if they click on the icon of my app, I want it to bring them back into the activity they left. How do I do that?
Edit
One thing I forgot to clarify (apologies) is that the activity the user left is not the main activity. I need to re-open the app in an activity that is normally not the main entry point.
To keep the system from launching another instance of your activity, you should add launchMode="singleTop" to your activity.
AndroidManifest.xml
<activity
....
....
android:launchMode="singleTop" />
What this will do:
When the user tries to launch your app (after the phone call, for instance), system will check if your launcher activity is already running && at the top of the stack. If it is, a new instance will not be created, and your activity will be resumed. Top of the stack condition is important. Depending on your requirements, you may want to opt for singleTask launch mode.
More info here: Link
I have it so when you click the notification icon on my app it opens up an activity. The thing is that if you click it over and over it opens the same activity over and over. So when you click the back button you have to go through all of those before going back to the beginning. How could I make it so that if that activity is already there it doesn't reopen it?
Check out the launchMode section of the manifest documentation.
You're looking to add:
android:launchMode="singleTop"
to the declaration for your Activity in the manifest. This means that if an instance of the activity is already at the top of the target task, the system will re-use that. Any intent gets delivered to the onNewIntent method of that activity.
I'd recommend having a good read of all the options on that page and deciding which one is right for your usage.
Try putting this in your manifest:
android:launchMode="singleTop"
I have this challenge with my Android APP:
I have and "dialog" - I mean an activity created as a dialog. Now, user presses the home button and leaves the app ~ later holds the home button and displays the Recent apps list. He will see the dialog as the last activity of my app. However, I don't want to display this activity on the "stack".
I tried the exceludeFromRecentApps (or similar name) and this one enabled removes whole app from the list. But I want to have dialog's parent activity displayed in the list.
I tried something with overriding the onResume, onPause and onStop methods, but no effect for me, since instance-level variables may be discarded (?).
I believe thats trivial problem, but I wasn't able to google working solution.
Lot of thanks!
android:noHistory="true"
android:taskAffinity=""
android:excludeFromRecents="true"
for your case, first you need to separate your dialog activity by "taskAffinity" tag. Then you need "excludeFromRecents" tag in you AndroidManifest.xml to remove is from recent list.
Like Hoan Nguyen said, you need to use the finish() method but know that the finish() method does not guarantee that the Activity will be finished.
So here is my activity.
<activity
android:name=".MainActivity_Hard"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/title_activity_main_activity__hard"
android:noHistory="true"
android:screenOrientation="portrait" >
</activity>
I marked it as android:noHistory="true" because I did not want the screen to be added to the activity stack.
But however when I lock the screen and unlock it. The activity disappears and the previous screen appears. This should not happen. Could anyone tell me why.
Note: It happens in ICS (Samsung Galaxy Note), but not with previous devices (Gingerbread,etc.)
Setting the noHistory attribute in your activity means that as soon as the user navigates away from that activity--bringing up the lock screen included--its finish() function is called automatically, thus destroying it and going back to the previous activity that spawned it.
This is typically used for splash-screen-type activities which, for example, acts only as a launcher and you don't want the user to be able to go back to the splash screen from the child.
If this is not your intention, then you should consider removing the noHistory attribute and maybe managing the activity lifecycle yourself. (For example, if you don't want your activity to be in the stack, then just call finish() just after it starts another activity.)
android:noHistory
Whether or not the activity should be removed from
the activity stack and finished (its finish() method called) when the
user navigates away from it and it's no longer visible on screen —
"true" if it should be finished, and "false" if not. The default value
is "false". A value of "true" means that the activity will not leave a
historical trace. It will not remain in the activity stack for the
task, so the user will not be able to return to it.
I don't know why. But if you don't want to add this activity to current stack. You can set android:launchMode="singleTask". When start this activity it will be added to it own stack.
You can see more here enter link description here
Hope it help :)
In AndroidManifest.xml put this permission tag above application tag.
<uses-permission android:name="android.permission.WAKE_LOCK" />