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();
Related
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.
I am new to Android . Here I have for activities A,B,C,D in which A is the Home Activity.It is in stack as A->B->C->D
When I press back from B or C it should go back just as normal. But if I press back from D it should go back to A and from A the app should exit
I guess you could intercept onStop() and guessing if the activity is switching to C and launching A instead. But it would result in a hard to maintain mess and I do not recommend that.
However, if for some reason you still not want to override onBackPressed and you manage to guess that D is stopping because back was pressed (without overriding onBackPressed(), just start A activity from there with an Intent with FLAG_ACTIVITY_CLEAR_TOP (call i.setFlags(FLAG_ACTIVITY_CLEAR_TOP) )
According to the doc:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
So A will be brought back and B and C will be cleared.
when you start your new activity finish the old activity for example if you want to start C from B :
Intent I = new Intent(B.this , C.class) ;
startActivity (I);
B.this.finish();
I have a base activity and some child activities like A, B, C and D. Here A is parent of B, C and D means that when either B, C or D finishes, control comes back to A. Now what I want is that when Activity say D finishes, it also close A (parent) and launch a new one say E. Here E should be the only on stack.
Its mean close all the activities that are invisible or waiting for result and launch a new one (E).
I don't know if this will work, but you could try it:
From Activity A, start activity B for a result using startActivityForResult()
set a result that will inform A to finish as well,
Call finish() in B.
When A receives that result from B, A calls finish() on itself as well.
and there you can start another activity.
for this you can set the request code for more than one activity which was start as for result and check into onActivityResult() to compare the result code
Try to register all the activity for broadcastreceiver.. When D is going to finish let it Broadcast, by listening to this finish all the activity and start new one before finishing anyone of the 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.
Support App starts activity A, then A starts activity B and finishes itself. After that activity B starts activity C.
Now the stack contains B and C, with C at the top.
Then I click a button in activity C, and want it to clear B and C and start activity A, i.e. I want activity A to be the only activity in the stack. How can I make it?
Edit: I made a test to use FLAG_ACTIVITY_CLEAR_TOP. But it didn't work in my case, because activity A is not running when button in activity C is clicked.
Set the FLAG_ACTIVITY_CLEAR_TOP flag on your intent to start activity A.
Edit: Is there a reason you can't leave A going? Then you could do as suggested.
Otherwise, another (more complicated) option:
In B start C forResult. When A is started from C, you could finish C with a result indicating to B to also exit.