Overriding onBack Pressed? - android

Correct me if i am wrong
"moveTaskToBack(false/true);" has nothing to do with the visibility of the Activity ,but has everything to do with Activity Stack,because many times on stackoverflow i find people being confused on this.
Now My Scenario:
there are two activites A and B
A is the root Activity
B is launched from activity A
the thing is i don't want my activity B to be killed after the back button is pressed,(just i want it to be invisible,and activity A to be visible which will happen on its own )so that i can restore its state afterwards.
so after searching a bit i came to know about
moveTaskToBack(false);,
which seemed to be the solution as it does not kill the activity (because the activity is not sent to back in the activity stack),but the only problem is.it works with Root Activity,and hence it will not work in my case.So is there any alternative which i can use with"non-root" activities,so that the state of Activity is restored....

I guess you have misunderstood
public boolean moveTaskToBack (boolean nonRoot)
Added in API level 1
Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged.
Parameters
nonRoot If false then this only works if the activity is the root of a task; if true it will work for any activity in a task.
Returns
If the task was moved (or it was already at the back) true is returned, else false.
Back Button by default takes you back to the previous activity. It pop's the activity from the back stack and the previous activity in the stack takes focus.
Save the state of the activity in onPause restore it in onCreate or onResume. To store values persistently chekc the below link for storage options.
http://developer.android.com/guide/topics/data/data-storage.html
http://developer.android.com/guide/components/tasks-and-back-stack.html
You may want to check the answer by commonsware in the below link.
https://groups.google.com/forum/#!topic/android-developers/4Pz6LrzVpx0

Related

Activities back stack seems to be lost

I have 2 activities: A and B
In B, I have a back arrow to go back to A by calling "finish()" method.
It works fine, except when the following is done:
I go to B
I put my app in background
I restore my app from "recent apps"
I press the back arrow, and then, the app is finished instead of going back to activity A.
Any help please ?
I figured out how to solve this:
As #Karthikeyan mentioned in its comment, setting launchMode to "singleInstance" is cause of the problem. I changed it to "singleTask" and it worked fine.
In fact, according to the google doc stated in https://developer.android.com/guide/components/activities/tasks-and-back-stack,
"singleInstance".
Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance.
So logically, with "singleInstance", the activity when restored from the background had no other activity in the same back stack, and thus, calling finish() would simply finish the activity without restoring whatsoever (the very first activity is in the background and in another separate task)
Do not keep activities make sure that this option is not enabled in your device's developer settings.
Set flag in onStop () method to know and write condition onBackPress () condition to navigate to A according to the Flag value changes
Make Activity A as parent Activity of Activity B in your manifest file. Works for you.

Calling finish() in onPause in child activity so that user refocuses into parent activity. Child activity gets recreated instead

I create a child activity "B" from activity "A". if the user should leave the app for any reason (most likely hitting the home button), I would like activity "B" to end and the app to be at activity "A" once the user resumes.
If I call finish() manually, activity B ends and it returns to activity A. This is the behaviour I would like to happen when the user leaves the app.
I have tried to call finish() in the onPause(), onStop() and in the onUserLeavingHint() of activity B. In each case, this appears to work correctly, and I can see mParent.finishFromChild(this); being called inside activity B.
However, as soon as the user switches back to the app, the onCreate() of activity B gets called and the user ends up in activity B.
How can I ensure I end up in the parent activity when I call finish() from within an onStop() (or similar) handler?
UPDATE: It appears that the issue is related to activity B being declared as using a SingleInstance launch mode. Removing this feature seems to have resolved the issue. Changing this has introduced other issues that I have since managed to fix.
The reason for this happening is that Activity B is set as a SingleInstance Launch Mode. The reason it was set to this (by another developer) is somewhat related to the reason I had wished the activity was ended when the app is in the background - it was to ensure the user could not reach this activity by hitting back on any other activities subsequently dispatched from Activity B.
To resolve this. I first ensured no activities could be created from B. To instead return from B and pass any required Intents on to A. Simplifying the back stack. (Calling activity B with startActivityForResult() is one possible way of doing this.)
Now, the reason SingleInstance causes this issue to arise in this scenario, is because Activity B is launched in a seperate new task. When the user attempts to resume, they re-enter this single-activity task. The rest of the app is running in a seperate task. The only thing the task can reasonably be expected to do is relaunch the activity. When the user presses back, the only thing it can do from there is to close the task (and hence appear to exit the app). For the expected behaviour to occur the user would have had to have selected the other, first task (through a long click of the task list).
Hopefully this self-answer can help someone who has encountered a similar issue.

Android back button behaviour

Let's say we have a default, empty activity with default behaviour, launched with default intent flags. User presses back button on the device. The activity disappear... but how, actually?
Is pressing back button behaving the same way like finish()?
Is the activity immedietely destroyed (onDestroy is called)?
Is the activity guaranteed to be destroyed, but not immedietely?
Is there any chance the activity won't be destroyed and this instance will be reused when this activity is launched in the future? (so only onPause and onStop -> onStart and onResume are called?)
I'm looking for a reliable answer, so please do not answer if you are not absolutely sure what happens here.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
This is a subchapter from the official Android documentation that addresses your question. It is a subchapter of the topic Managing the Activity Lifecycle, which can be read here:
http://developer.android.com/training/basics/activity-lifecycle/index.html
It is definitely worth reading the whole chapter to get to know the details about Androids Activity behaviour. But the subchapter ( first link ) is the relevant part to this question.
you use should look into this try this
and please tell specific what you wish to do with back button for your default activities ......
When you press back, (if not intercepted by anything like the keyboard, fragment, activity, etc) the OS (via ActivityManager probably) will try to show the user the previous activity in your current task (again, ignoring fragments' back stack).
If there is no such activity, the task will be terminated and you'll go to the previous task - the home screen most of the times or some other application that might have launched your app.
You'll get onDestroy called soon (it depends on how long it takes to start the next activity but on a good phone it should be under 100-200ms).
Your activity instance won't be reused after onFinish. This happens before the activity is destroyed so if you need another activity of the same type, the OS will create another instance.
When the user presses the BACK key, the current activity is popped from the top of the stack (the activity is guaranteed to be destroyed, but not immediately, may be when the system resources are low) and the previous activity resumes (the previous state of its UI is restored).
Which actions does the back button/back key on Android trigger?
Definitly onDestroy() is called .....There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish().

Android; How can I detect whether a Parent activity is still alive in Activity stack and then receate it

I have activities in sequence as Activity A calling Activity B,When I am on Activity B I press Home button and start another Application (for example Map). If i stay on this second application for a long time say 5-10 minutes and then press Home Button again and choose to Start my application again, then Activity B is started again and works fine. But when i Press Back key - I return to my Activity A again (which is also correct) but it shows a Blank Screen. Ideally in correct version it should show me Acitivty A with the XML data is ListView form.
Alternatively, in the above description, when the other Map Application is started and if the user stay on it only for 1-2 minutes then the above problem doesnt not occur.
Can anyone suggest a solution on the same.
Is it that i need to check in Activity B whether Activity A is still there in Activity Stack (How do i do the same) and If its not in my Activity stack then re-create it.
I tried doing alwaysRetainTaskstate in my Android manifest file for Activity A. But it doesnt work at all
You don't have to do any of that, Android takes care of the technicalities for you - it will recreate your Activity A.
You just need to remember to save A's state when B is opened (take a look at Activity.onSaveInstanceState). When A is restarted, your saved state will be passed to onCreate.
You should really read about Activity Lifecycle
This should help.

help calling an Intent / activity that has already been created

I have two buttons on my application. One button starts a new Game (lets say solitair).
When ever this button is pressed it will always start a new game
My problem is that I would like a second button to go back to the game that has already started if the user clicks back.
How do I go about recalling that activity that has already been created
Thanks
I assume that the game is from a third party. As I see it you don't even need second button. Just click the first one again and you will get back to the game.
If the game intercepts Back it may intentionally destroy it's state so there is no way to jump back to the place that you were before hitting Back.
You have to save the state of the activity
onSaveInstanceState() and onRestoreInstanceState() are used to handle the activity state
onSaveInstanceState() method gets called for an activity when user leaves the activity. Note this method is only called when activity is present in the History state and user can come back to the activity.
onRestoreInstanceState() restores the state of the views.
see the following link
How to manage activity state
You have to read manual about activity stack
Your application can has more than one task and more than one activity back stack

Categories

Resources