Pseudocode :
The initial Activity starts. (ActivityStartScreen)
A button is pressed that starts a new Activity (ActivityOtherScreen) by using an Intent.
The new Activity has a button that loads the initial Activity.
My question is, is the original activity gone once it 'loses focus' (when the 1st button is pressed), or is it stored somewhere, and is there a way to retrieve it?
Currently, Im re-creating the original activity with an intent. I bet this isnt the proper way.
The previous Activities are stored in the Activity stack, to return to previous activity, just call finish() on current activity. Note that this way you lose the 2nd activity.
You simply must call the finish() method from your second activity to go back with the states all being the same.
Here is a helpful warriorpoint blog post tutorial that will walk you through it.
Related
In my app I have situations where I need to get a user back to some activity that preceded(not necessarilly directly) the current one. All of those previous acitivities might need Intent parameters in onCreate.
So, my question is there any easy way to get user back to an activity that might not be the direct previous activity he's been on and is it possible to avoid manual workaround of saving/restoring those previous activities' intent parameters ?
Consider an example: there's a global search-bar that can provide users with suggestions on products; once they hit one of suggested items they get moved on a product-view activity where they can reload this activity with another product - walk through. After a couple of such reloads they might decide to go back to the activity where the search was initiated, but it might not the closest to the current one.
UPD: There also should be a possibility to go back in B activities sequence.
Using startActivityForResult() while loading a new activity and using finish() to close the launched activity on back press can solve your problem.
As Android newbie I started to wonder about the Activity lifecycle. I'm having an Activity that loads a list of Persons and displays them. Upon the click of a Person I want to open another Activity showing the details of that Person. I'm currently doing this by creating an Intent on the "PersonDetailActivity" which I then start. So far so good. On the PersonDetail page I would like to have a menu action to go back to the Person list. I again applied the same technique, meaning an Intent that brings me back to the PersonListActivity.
Now I started to wonder what returning to the PersonListActivity means. Will a new instance get created and will I have to reload the persons that it displays in the list? Can you come back to the same instance, avoiding having to reload the list again? Do you then have to pass a pointer to yourself via the intent to the other Activity?
So when will and Activity be re-instantiated and when will it not. Any hints or suggestions are more than welcome. Maybe there are some patterns to be applied for these back and forth menu actions that I'm not yet aware of.
Thanks,
Vincent
Yes,,. Call finish() in second Activity instead of starting new Activity..
There is basically something called Activity stack which stores all Activities in the order they were started.. so if start new Actvity , that sits on top of the stack and preveous one gets below it.. when you call finish the Activity is poped out..
if you don't want to call finish() correct waht ever you were doing then add flag ACTIVITY_CLEAR_TOP in manifest for the 1st Activity..
Basically if you just call the finish() method on your PersonDetailActivity
PersonDetailActivity.this.finish();
it will activate the onResume() method from the Activity that is on the top of the finished one, which here would be your PersonsActivity. You can specify in your onResume() method what you want to perform when turning back there.
I have a problem cause when I go to different activities using startActivity function,
they always get created from scratch. Even if activity A was visited before, when I go to activity B and then A again, activity A is created again.
The problem is with back button, cause if I go to Activity A then B then A and then B,
in order to close the application I have to press back button 4 times.
I guess that it shouldn't act like it and user should be able to go to activity A when first pressed back button and the second press should close the application.
How to solve this issue?
Greetings
If you have activity transitions like:
Activity A -> Activity B
Activity B -> Activity A
and you want the user to go back to the same instance of Activity A in this case, maybe you just need to call finish() in Activity B after you call startActivity() for Activity A?
If this isn't helpful, please give us more information about what you are trying to do.
make sure you implement onSaveInstanceState and be prepared to restore your activity from a Bundle in onCreate. that's how you re-establish where you were when you return to an activity.
add launcheMode="singleTask" to your activity in the manifest
You need to set FLAG_ACTIVITY_SINGLE_TOP to your intent for launching activity A. Doing so will cause your previously created activity to re-use. Make sure you do handle your afterwards intents in onNewIntent method. For more info.
You need to set the flag FLAG_ACTIVITY_REORDER_TO_FRONT when you start activity A from B or vice versa, like
i = new Intent("....ActivityAorB");
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
I've tried solutions proposed so far, however they didn't do it for me.
What did however, is using flag FLAG_ACTIVITY_CLEAR_TOP while starting activities.
Thanks for pointing me in the right direction though.
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
I am totally new to android development....what piece of code shall I write to move back to the last activity from the current activity on a button click?
I use intents to switch between the activities but is there anything specific to resume back on the last activity?
Please advise...
Thanks,
Pressing the back button should do that "out of the box"...
If the user clicks on Back button, application will automatically take you to previous Activity. You don't need to do anything.
If what you pretend is to implement that action from current Activity, for example through a Back menu option, just call finish() in your Activity. You can call setResult before that, if you called that activity with startActivityForResult
Ger
By default back resume previous activity.
You should read this to better understand android Activities:
http://developer.android.com/guide/topics/fundamentals.html#lcycles