I am starting a new activity with thsese flags
finish_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
when i close the activity it resumes the activity before that how can i close all the activities ?
when you move from the 1st activity to the next activity , the previous activity gets saved on the back stack, this adds the overhead, as objects of same activity are created again when u return back to the previous activity. So you can use the method finish() to kill the 1st activity when moving to the 2nd.
eg:
Intent i = new Intent(this,Myapp.class);
startActivity(i);
finish();
When starting a new activity finish the previous one
Intent i = new Intent(this,CLASSTOLAUNCH.class);
startActivity(i);
finish();
Related
I want to execute two activities from one activity, where One should go in Pause state and other will go in Running mode.
Start the 2nd Activity from the first Activity.
Start the 3rd Activity from the onCreate of 2nd Activity.
Now you will see 3rd Activity and on closing this activity 2nd Activity will show up.
Use this:
Intent in = new Intent(getApplicationContext(),
PuasedActivity.class);
startActivity(in);
in = new Intent(getApplicationContext(),
RunningActivity.class);
startActivity(in);
When you press the back button the paused activity will start.
In my android app i have two activities, such as Activity a and Activity b. I want to close Activity a as well as Activity b from Activity a, I tried the code below, but an exception occurs,
a.this.Finish();//To finish current activity works fine
b.this.Finish();//Exception occurs because i tried to close from a Activity class.
So how to finish Activity b from Activity a? Guide me,
You need to start activity b for result and close it when activity a is closing:
Start of activity b:
Intent it = new Intent(a.this, b.class);
startActivityForResult(it, REQUEST_CODE); // REQUEST_CODE is int value
Finish of activity b:
finishActivity(REQUEST_CODE);
Activity A to B
Intent b = new Intent(A.this,B.class);
startActivity(b);
finish(); // Activity A will close it before starting B Activity.
Activity B to A
Intent a = new Intent(B.this,A.class);
startActivity(a);
finish(); // Activity B will close it before starting A Activity.
To finish() an Activity, you need to get this instance. But I think that if you want to control many Activity, I suggest you to use fragment. I meet your problem before and try to fix them but I meet more issue with that. So let try to use Fragment.
I have some sets of activity.
Home Activity -> Activity 1 -> Activity 2 -> Activity 3 -> HomeActivity
finish(); finish(); finish();
Home Activity -> Activity 1 -> Activity 2 -> Activity 3 -> Activity 4 -> HomeActivity
finish(); finish(); finish(); finish();
So now when I am on the final step that is on the HomeActivity if I press back button it again takes me on to the home activity.
How do I finish the home activity without disturbing the whole process? Any help appreciated.
use this flag with your intent in each activity
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This will not launch home activity again . It will use the previous instance of you Home Activity and bring it to top.
with CLEAR_TOP If 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.
You can refer this Link to get more idea about flags.
Whenever you switch from one activity to another by defalut onPause() will be called, if you dont want this activity(in your case home activity), just override onPause() in your home activity and call finish() in onPause().
Allright whenever u call your HomeActivity just add this to your Intent
Intent i=new Intent(yourActivity.this,HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
This will clear any previous present activities stack. Hope it helps.
while calling Activity 3 -> HomeActivity
finish()
implement below code :
Intent i = new Intent(Activity3.this,HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
call finish on the first home activity as well. i.e when you start activity 'Activity 1' after startactivity() call finish also.
Intent i = new Intent(Activity3.this,HomeActivity.class);
startActivity(i);
HomeActivity.this.finish();
I have 3 activities in my application
ActA
ActB
ActC
Suppose I am in activity ActB and I am loading ActC with out finish(); ActB
Then when press a button in ActC , need to redirect the application to ActB . But this time when I press back from redirected ActB , another ActB ( previously loaded ) is showing.
Is there any way to kill all the activities which are previously loaded when we press the button in ActC ?
I am new to android and its ruining my time
Please help
thanks in advance
When you launch ActC from ActB, do so with this flag on the intent:
Intent intent = new Intent (this, ActB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Javadoc:
"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."
Just going from ActB to ActC, use Intent and finish() after calling the Intent
Intent intent = new Intent(this, ActC.class);
startActivity(intent);
finish();
And then if you want to go back to B from C, then do the same in reverse, so switch
Intent intent = new Intent(this, ActB.class);
And the rest is the same.
Suppose you move like this
A -> B -> C
All the previous instances will be there in backstack for previous activities.
until and unless it is your requirement to create new instance of activity then only do so.
when you press button in you want to come to B but if you don't need new instance of B you can go with backstack item and according to me you should.
in button click you can simply call onBackPressed() of activity which is called when you press back button of device.
Also as Vee said you can use that flag to clear activities above your current activity.
If you want to "kill" the activity you should call finish();
To achieve your goal you can do the following thing.
When starting ActB from ActA, after calling startActivity(...); put finish();
This way you killed Activity A, do the same in ActB when calling ActC. Then when you call ActB from ActC again, it will start a completely new activity.
If you don't need a new instance of B then you can simply call finish() in your onClick() of C and this will take you back to B and no need for Intent or any other code.
If you need a new instance of B then you can use Vee's suggestion, keeping in mind that this will clear Activities off of the stack if you add more in between.
If you don't need a new instance of B but want to pass data back to it then you can use the flag FLAG_ACTIVITY_REORDER_TO_FRONT
Intent i = new Intent(ActC.this, ActB.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
// can send data to ActB if needed with putExtras()
startActivity(i);
finish(); // if you want to destroy C and take it off the stack
this will not create a new instance of B but bring it to the top of the stack so when you press the "Back" button, you will not have the second instance on there.
When user presses button in ActC to goes back to ActB (by creating a new ActB) do this:
Intent intent = new Intent(this, ActB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will finish both ActC and the previous ActB and create a new ActB.
Okay say your using a app, and you opened a new activity and then opened another, you can end the activity your on by using finish(); and your back one activity, but how can you go back two activities, all the way back to the first one? I know you could use:
Intent savedGameIntent = new Intent(v.getContext(), firstclass.class);
v.getContext().startActivity(savedGameIntent);
But is that the best way to do it?
Use the flag Intent.FLAG_ACTIVITY_CLEAR_TOP.
Intent intent = new Intent(this,A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
From the documentation:
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 effectively, if you have A -> B -> C, and you intent to A with that flag set, B and C will close.
I believe that will start a new activity, not back up to the original one. It sounds like you want to finish the last and middle activities. If you start the last activity with startActivityForResult, you can then override onActivityResult in the middle activity, and call finish() from there.