Go back to Activity without recreating (without invoking onCreate()) - android

I would like to ask similar question to:
Go back to previous screen without creating new instance
However I don't want Activity A go through onCreate callback, I would like to return to previous instance in the same state. How to achieve that without calling multiple finish() ?

There's no need to finish() activities as you navigate through your application. Instead, you can maintain your Activity back-stack and still achieve your goal. Let's say you have 4 activites like so:
A --> B --> C -->D
where D is the topmost activity and A is root activity. If you are 'falling back' to activity B, then you'll need to do two things in order to avoid hitting B's onCreate method.
1.) Make B a "SingleTask" activity. You can do this in the Android Manifest. To be brief, this means that only one 'instance' of B will ever exist within this Task. If B is already running when it's called then it will simply be brought to the front. This is how you do that.
<activity
android:name=".ui.MyActivity"
android:launchMode="singleTask"/>
But you don't want to just bring B to the front. You want to 'fall back' to B so that your stack looks like
A--> B
2.) Add this flag to your intent that 'starts' B. This ensures that C and D are removed.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Now when 'D' calls new Intent to B, B will be resumed and C and D will be removed. B will not be recreated, it will only call onNewIntent.

When navigating from A to B do not finish Activity A.
When navigating from B to C finish Activity B.
When navigating from C to D finish Activity C.
So from wherever you navigate back finish current Activity and Activity A will get resume again not get created.

Related

Intent flags on Android

I have a widget for my application, which need to be somewhat independent from the app.
The activity workflow should be like this:
Widget -> Activity acting as receiver
Receiver -> LoginPage or Activity A (depending on login status)
LoginPage -> Activity A
Activity A onKeyDown -> Activity B
Activity B onKeyDown -> Home Screen.
I have no problem until Activity B, which sends back to Activity A when I press onKeyDown. I'm using FLAG_ACTIVITY_CLEAR_TOP flag and finishing the Activity when starting the activity B.
When I move from ActivityA to ActivityB using the CLEAR_TOP flag, I supposed that Activity stack is cleared, then in ActivityB I finish the Activity on the onKeyDown() method, assuming that the App will be closed, but it doesnt. Why?
I'm also trying to use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK in the receiver but I dont understand the mechanism pretty much. Any idea about this?
In fact the FLAG_ACTIVITY_CLEAR_TOP, start your activity B if its not started or it came back as the second activity on the BackStack. To finish Activity A, you can call finish() after starting Activity B or add no history flag, when starting A.
#JesusS: I doubt if u can finish ur activity in that fashion during a forward transition.
Consider a scenario of moving from Activity A to Activity B. Now if u want to kill Activity A and want to move to Activity B then call the startActivity(intent);
(where ur moving from activity A to B)
without any flags on the intent followed by the finish() on activity A.
As per my understanding u can use Intent.FLAG_ACTIVITY_CLEAR_TOP only during backward transition i.e when u already have that activity on the stack.
Consider the following scenario:
A --> B --> C --> D
Now if u want to move back from activity D to Activity A by clearing the activities u can go for Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.
The result is that the Activities D, C, B(LIFO) will be removed from the stack and the activity A resumes by calling the onResume() of Activity A.

How do I collapse "child activities"?

Example: I have an android app with 3 activities that has the following behaviors:
A (Home) -> B -> C
Activity A launches Activity B
Activity B launches Activity C
When user is on Activity B and they hit the Back button, it takes them Activity A
When user is on Activity C and they hit the Back button, it takes them Activity B
What I would like is when user is on Activity C, if they hit the "My Root Activity" button, it will take them to Activity A without adding a new instance of Activity A to the back stack.
So I don't want to have:
1) A
2) A-B
3) A-B-C
4) A-B-C-A
What I would like is:
1) A
2) A-B
3) A-B-C
4) A
How can I do this?
Check out the intent stack machinery!
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
EDIT:
You can add "singleTaks" or "singleInstance" to your activity in the manifest file and implement the onNewIntent() method.
"singleTaks"
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
"singleInstance"
Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.
Check out this link
I have each child activity extend a base activity. In the base activity, I defined an onActivityResult() which will impose a finish() if the activity at the top of the stack set a specific resultCode. So if they hit the "My Root Activity" button on Activity C, it will recursively roll up to Activity A. The Back button maintains it's functionality.
This is a rare scenario as I would have used "singleTask" but launching Activity A involves reloading it's dependencies which I don't want to couple of Activity C.

What is the difference between Intent.FLAG_ACTIVITY_CLEAR_TOP and finish in android

What is the difference between Intent.FLAG_ACTIVITY_CLEAR_TOP and finish() in Android?
The differerence between these two are as follows:
1.finish() you can use to end the activity in which you are right now present and also it will end one activity at one time.
2.In case of FLAG_ACTIVITY_CLEAR_TOP,It will end all the activities those are on top of the current activities inside the stack.There may be more than one activity.
suppose you are starting activities one after another in the order
A-->B-->C-->D,ie activity B started from activity A,activity C started from activity B and so on.
Now calling startactivity(A) from activity D with intent flag FLAG_ACTIVITY_CLEAR_TOP finishes all activities in between (here B and C) and starts A.
calling Finish() from your activity closes current activity
finish() android uses to end the activity by calling it in program.
(Note, you can also use onDestroy()).
FLAG_ACTIVITY_CLEAR_TOP clears all the activities that are top of the current activities inside the Activity stack.

Return result to previous activity that is not the last activity

The startActivityForResult()and onActivityResult works perfect if there are only two activities are involved. But how can I handle this, if more than 2 activities exists?
Example:
Activity A starts a new activity B, that starts activity C, that starts activity D. I want to return the result of D to activity A along with finishing activities B and C. How can I do this? Can I loop through the activity stack and finish the wanted activities or must I start a new instance of activity A?
For short: A->B->C->D has to lead back to A with the result of D.
Going back closing each activity would be a good way of doing things, but if you need to jump from an activity to another and you're not using a TabHost, you could take a look at the APIDemo Reorder code
It jumps from an activity (4th) to a previous opened one (2nd) in this way:
Intent intent = new Intent(ReorderFour.this, ReorderTwo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
In my opinion the most logical way is have the Activities take responsibility for this.
D returns d to C.
C returns d and c to B.
B returns b,c,d to A.
This will force you to consider the error conditions when the Activities don't happen in this cycle explicitly.

Moving between activities: killing two previous activities

I'm developing an Android 2.2 application.
I'm very new on Android, and I see that if Iaunch a new Intent from an activity, this activity goes to paused state.
If I want that user can't goes back to this previous activity, what must I do? May I kill this previous activity with finish?
UPDATE
An example:
A, B C and D are activities.
A is the first activity.
A launches B, and B launches C, and C launches D.
I want to close or kill activity B and C when D is launched.
Thanks.
You can use your intent to startActivity like normal and follow the startActivity with
finish();
If you do this on your hypothetical activity B, you can hit the hardware back button in C and it'll take you directly to A.
You might want to rework your design. What if you have A call startActivityForResult(B, 0), and when B would have launched C, instead setResult and finish. Back in A, onActivityResult gets called, with the result you set. You can use that result as a message for A to now start the activity C.
See if that helps - http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP (This and other flags)
And also you must invoke
finish();

Categories

Resources