So, my app has 5 activities: A, B, C, D, E. A is the splash (launcher) activity and never gets called again after launch. After launching, B gets called and serves as the Home activity. B, C, D, E all get called with Intent.FLAG_ACTIVITY_REORDER_TO_FRONT so that one instance is reused and updated through onNewIntent(). It works. I'm happy with the user experience.
Here's my question. I would like the first instance of B (upon launch) to hold as the root task. In other words, I would like this first instance of B to be the last screen the user sees if pressing the back button continually until exiting the app. All other of instances of B should be "recycled" normally using the Intent.FLAG_ACTIVITY_REORDER_TO_FRONT.
I'm pretty certain I can make a unique activity (call it B2) and accomplish this, is there a better way?
You can use
Intent cActivity = new Intent(BActivity.this,CActivity.class);
cActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(cActivity);
setting Intent.FLAG_ACTIVITY_NEW_TASK will hold your B Activity's instance and open your Activity C as new task. or you can call startActivity() without setting any flag.
Related
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.
I'm new to Android development and I have an app with various activities. For performance reasons I'd like to properly manage the activities when users are using my app. Here's my problem.
Activity A - starting activity with map
Activity B - user navigates to Activity B (a list view) from Activity A.
The user then selects the map icon to navigate to Activity A again.
So if you can image it, my activity stack is now:
Activity A
Activity B
Activity A
So if I press the back button the device it will take forever as it scrolls through activities.
Is there a way of managing this so the old activity is destroyed and is just re-created upon choosing an activity nav icon. I've read up about onDestroy() and onStop() but I'm a little confused at their implementation.
Apologies for a poorly worded question but I'm unsure of the correct lexicon to ask about activities.
One simple solution is to kill the Activities as soon as they leave the foreground.
You could do that by calling finish() inside onPause().
You could have B finish itself and return to A instead of starting another A. Or, if your stack might be more complicated, like this:
D
C
B
A
D could start A with FLAG_ACTIVITY_CLEAR_TOP, which would cause D, C, and B to be finished, leaving A on top. (That intent flag interacts non-intuitively with a couple other flags, so read the docs.)
I was going through Android Task and Back Stack documentation and at one point they mention this:
if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and pushed onto the stack (rather than bringing any previous instance of the activity to the top). As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. As such, if the user navigates backward using the Back button, each instance of the activity is revealed in the order they were opened (each with their own UI state)
Let's take an example:
I have Activity A Starting Activity B which Starts Activity C which starts D.
Stack is A->B->C->D now it is possible to Start C from D so when we start C from D stack will be
A->B->C->D->C
Now instead of this standard behavior I want Activity to have only 1 instance or only 1 entry in the Back Stack. "SingleTop" will not work since Activity C was not on top when we started it from D.
I might be missing something but is there any way to achieve this making sure an activity has only 1 backstack entry?
Thanks
Pranay
Use Intent.FLAG_ACTIVITY_CLEAR_TOP, e.g.:
Intent intent = new Intent(context, <your_activity_here>);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
So, all the activities in stack after activity C will be finished automatically. If you use the specified flag
A->B->C->D
will become
A->B->C
You can also use android:launchMode="singleInstance" in your activity tag in manifest
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.
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();