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.
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.
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
This is the scenario. I have 2 activities in my application and a Dialog activity that is started when I click on the notification created by my application.
The problem is that when I click on the notification, only the Dialog should show, not the other activity of my app, if it was stopped on pressing the home button.
When I close my application by pressing the back button, the dialog activity shows the dialog, but when the application is running in the background, that activity also opens up on creating the dialog activity.
I use #android:style/Theme.Dialog for my dialog activity.
How to only show the Dialog activity, not other activities in the backgroud?
The solution to the OP issue is setting a different Task affinity for that activity in the Manifest.
<activity android:name="MyIndependentActivity"
android:theme="#android:style/Theme.Dialog"
android:taskAffinity="a_unique_id">
The taskAffinity string must be different from the package name (com.whatever.myapp)
I also use android:excludeFromRecents="true", to hide the dialog from the recent apps list, as it usually makes no sense returning to a dialog.
More info: http://developer.android.com/guide/components/tasks-and-back-stack.html#Affinities
Yeah that'll happen.
When you declare a theme of Dialog it affects the activity lifecycle and the previous activity doesn't go into onStop so some Android functions still think it's the active activity which is technially true as your dialog Activity is acting like a dialog.
One possible work around if you don't 'care' that you can see the previous activity behind the dialog is to change the dialog to be a DialogFragment, put the theme of dialog on the fragment and show this in it's own activity, that'll do it.
The way I have done this (and this can't be the best way to do this) is to have some logic in the activity.onPause() and activity.onResume() methods, that will perform actions based on what I want. My experience is more around separate activities and transitioning between them than using dialogs alot.
You can pass information between activities through setResult(). This will enable you to work out why the child activity has decided to close. That combined with the onResume function should enable you to disable the parent activity.
To override the dialog so that the other activity is not visible behind it, is probably to use the onPause() method to make it go translucent.
I have found onStop() very irratic to use and often unnecessary. The reason for this, is that it is called unpredictably from a developers point of view because onStop can be called based at strange times based on whether the OS has enough memory etc. onPause however in my experience is always called predicably.
I would like to launch always a specific activity, not this which was active during closing application. I don't have possibility switching to desirable activity before closing, because it could be dead.
I would prefer even delete shortcut to whole program from recent app than make user confused by launch credits instead of start splash screen. However this would be only workaround (but just in case, how can I do that?)
I decompiled some apps and found their solution:
<android:name="..."
android:taskAffinity=":shortcut"
android:excludeFromRecents="true"
android:clearTaskOnLaunch="true"
...
About android:taskAffinity:
An affinity name that applies to all activities within the application, except for those that set a different affinity with their own taskAffinity attributes. See that attribute for more information.
By default, all activities within an application share the same affinity. The name of that affinity is the same as the package name set by the <manifest> element.
If you mean you want to start your application through the same activity every time, add
android:launchMode="singleTask"
to your main activity in the manifest file. This will force your application to put this activity at the bottom of the activity stack clearing all other activities which may have been running.
One way you can achieve this would be to mark all your activities with android:excludeFromRecents="true" attribute. this would ensure that none of your activities shows in the Recent Applications list.
You should also look into the android:finishOnTaskLaunch and android:stateNotNeeded attributes.
The correct way to solve this is to add
android:noHistory="true"
to the manifest entry for all activities except for the main (root) Activity.
When the user returns to your application, either from the list of recent tasks or by pressing the app icon on the HOME page, all activities (except for the main (root) Activity) will be removed from the task (actually, they get removed immediately when the user navigates away from the app by pressing the HOME button, answering an incoming phone call, choosing another app from the Notification bar, etc.
I'm experiencing kind of strange behavior of my application after hard Home button is pressed.
When you press Home, everything is OK - my app goes to the background, showing Home screen. But if you try to choose my app in the main menu or in the list of last tasks it behaves like it was not started before and does not show the last activity you were on - it just starts from scratch, namely, shows the splash screen and starts next corresponding activities. Moreover, old activities of this app remain on the activities stack, and previous instance of the app is not terminated - so if you press Back for a few times you'll just run into those activities which were undoubtedly started during the previous session of work with my app. Splash screen activity is filtered by "android.intent.action.MAIN" filter and "android.intent.category.LAUNCHER" category.
The strange thing is that all of that happens despite the fact that I do not intercept any Back key hits, or override any onPause or onResume methods. What's happening contradicts with my understanding of Android app lifecycle - I was sure that when you hit Home an app just goes to the background, and when you choose it in the menu later - it just unwinds and does not start anew. (Of course, unless stuff like that is stated in the app manifest or corresponding methods are overridden or something else).
I also checked it for some other lifecycle events - such as changing orientation or flipping hard keyboard out - and none of those led to such strange results. It appears that the problem occurs when you try to start the app from main menu or menu of last applications.
I hope you will be able to help me. Any advice on what to pay attention to or where to search for solution would be really great.
Regards, Alex
You need to set android:launchMode="singleTask" in your LAUNCHER activity in your manifest file.
For more info on the launchMode attribute see here
Note that:
The default mode is "standard".
and:
Every
time there's new intent for a
"standard" activity, a new instance of
the class is created to respond to
that intent.