I have an app which has 4 activities in it.Within app, history activities, i.e. Activities from where I navigated should not be destroyed , so I don't call finish() when I am navigating.
But when I press HOME button I want to kill all activities , So that When I come back to the app , Index screen or say first activity is displayed instead of previous paused activity.
Problem here seems to be, how to differentiate between backs within an app to HOME button.
I saw few answers regarding this in other questions. Got more confused.
Is there a way other than intercepting HOME KEY PRESS, because as suggested in other threads,
I should not override HOME key press (as it might have side effects)
Set android:clearTaskOnlaunch="true" on the activity launched from the home screen.
You might also check some of the other attributes you can specify on activity, to tweak it's behavior a bit more.
On main activity add:
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
On the others add:
android:finishOnTaskLaunch="true"
This way it will kill off any activity when returning to the app after being in background.
Related
When HOME button is pressed activity goes pause state
but if the activity is NOT a launcher or MAIN activity then I want to find actual reason why the top activity goes pause state.
Would someone suggest how I can get pause reason from a non-launcher/ non-home activity which is on top before goes to pause due to HOME button press action?
That's ecpected behavior. The HOME button brings up the launcher app. That means the launcher app's activity is now the foreground activity. There is only one foreground activity in Android at a time. That means whatever was previously the foreground activity will be paused, then stopped (unless for some reason part of it remains on screen, which would be weird but possible with a 3rd party launcher I suppose).
Why don't you think it should be paused? I think you don't understand how activity lifecycles work.
Will be obliged if someone can help me with this.We were working on some android application,while designing its prototype we came across some issues.A little description of the application's prototype is as follows:
A Log in screen
a Home screen with logout at its top right
Every other activity is having a home button on top right corner.
Now going from one activity to the other in my application, I just called finished on the current activity and started the other(so there is no stack of the activities is being created) and when home button present on each activity's top corner is pressed i finish the current activity and move to the home screen.
More precisely,i can say that i am overriding every activty's onBackPressed() method
.By doing this i am not letting android to keep a stack of the activity's but by doing this I have a feeling that, I am loosing efficiency and degrading performance.Because on every backpress or home button click one activity is finished and the other is created.Hence some lag can be seen as the acivity is recreated.
Please suggest that should I continue WITH THIS or there is some other way out to handle this
Thank you for Giving your time
It is depends.
If you are giving option on each screen to to go home this approach is good.
Because if you are keep activities in stack it will be in RAM which is limited so it may create memory issue if to many activities in stack.
I would like to suggest one more thing.
As you say you have overridden onBackPressed() method in each activity.
Rather doing this there is a option you can specify parent activity in manifest tag.
So you no need to manually handle by overriding onBackPressed() method.
EG.
<activity
android:name="com.xxx.DetailActivity"
android:parentActivityName="com.xxx.src.ListActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
So I have my main activity, and when you press a button it launches activity2. Once activity2 is launched I press the home button and it will trigger onPause at activity2 and then the desktop of the android device comes to top. Everything's ok.
The problem is: now I click the app icon and instead of coming to top activity2 (the last active), it comes my main activity. So onResume is not called on activity2, but on my main activity.
Isn't it supposed to be called onResume on activity2? How could I make it work?
If you launch an application from the home screen launcher, you are specifically launching the activity which that launcher icon corresponds to.
The behavior you are thinking about is more along the lines of what occurs when you re-enter by way of an activity which is in the back stack.
If you want the home screen launcher icon to put you back in whatever activity you were previously in when that is different from the activity that icon is linked to, you'll need to keep track of that and have your launcher activity transfer you there.
Just to be sure, didn't you override the onKeyDown method which is able to catch device buttons?
Remove the android: launchMode="singleTask" from your launcher activity in the manifest file.
I've searched SO and found several answers to the question in general, and have tried them all and am not having success. I really don't have my head around how the back stack works, Intent flags or the finish method. Here's my setup:
On application start-up, there's a splash screen where a couple AsyncTasks run in the background and check a couple webservers for updated content. ProgressDialogs report status. When complete (via the last onPostExecute), I launch a new Activity ("Home"). This seems to reflect some of the other posts, but I think my kludge is due to Home being a TabActivity, with 4 tabs, that initially calls setCurrentTab on tab 0.
So, using the suggestions previously posted:
android:noHistory="true" on the Splash activity
calling Splash.this.finish() after it launches the Home TabActivity
setting the Home TabActivity intent flag of Intent.FLAG_ACTIVITY_CLEAR_TOP
setting the Home TabActivity intent flag of Intent.FLAG_ACTIVITY_NO_HISTORY
The users sees the splash, the TabActivity launches, the user clicks to another tab, then hits back - the application closes (not force close - just closes back to the devices home screen).
If I don't use any of those, when the user hits back after changing to another tab, they go back to the Splash screen and are stuck (I could add a button or something to take them to the Home TabActivity but that's not optimal).
The desired result is that the user sees the Splash, gets taken to the Home TabActivity, clicks another tab, then hits back, he should be taken back to the initially set tab (tab 0).
Any insight is appreciated.
TYIA
The back stack is actually officially called the activity stack - every time you start an activity, that gets pushed onto the top of the stack (unless you set one of those flags you mentioned).
This means that unless each tab in your main app is a separate activity, then the default back key behaviour will be to leave your main app activity.
You can control this by taking over the back key or by overriding the tab switching behaviour to start different activities.
I have read into the finish(); commands and the FLAG_ACTIVITY_CLEAR_TOP commands and also checked out Common Ware's answer on killing app, but I am not sure how to put this into my app.
Basically, I have a user click a button that takes them to the camera. The user then snaps a photo and it brings them to a layout view. The user then clicks a button that takes them to one of 2 views, depending on a some conditions.
The user is then allowed to either retake a photo, or go to the main menu (depending). My problem is, if the user goes back to the main menu, and snaps another, then another, etc...the activities stack, so when I click the 'Main Menu' button the app goes back through eached stack activity until finally it goes back to the main menu. Is there a way to kill each activity with one of these lines, so even if a user retakes a photo, they will only need to go back once to get to the main menu?
Thanks!
I use the noHistory parameter in the manifest to accomplish this. Here is an example of a manifest entry for a Activity that should not be placed in the history stack:
<activity android:name=".MyActivity"
android:label="MyActivityTitle"
android:noHistory="true" />