Finish an activity in android? - android

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();

Related

how to close android app when back button pressed

I am building a simple login application on android platform.
The 1st Activity is a Login Activity and the 2nd one is a welcome screen.
When a user presses the login button, I start the 2nd Activity by using an Intent.
But in the 2nd Activity, when the user presses the back button, it opens the 1st Activity.
But at this point I want to close my app.
Can I use Fragments?
Any other alternative?
you have to just finish your activity when you switching another activity,
Intent i = new Intent(Login.this, Welcome.class);
startActivity(i);
finish();
you call the finish() in LoginActivity when you start the your SecondActivity
Intent i = new Intent(Login.this, Welcome.class);
startActivity(i);
finish();
OR
you set the android:noHistory = "true" in LoginActivity in AndroidManifest.xml.
you need to create your second activity with FLAG_ACTIVITY_CLEAR_TOP from your first activity.
and then in your second activity you need to call finish() in onbackpressed()
#Override
public void onBackPressed() {
finish();
}

How to close one activity to another

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.

Android exit from previous activity

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.

How to Clear all previous activities~?

Activity Flow in my app.
WelcomeActivity -> SignInActivity -> SignUpActivity ->
TabsActivity(this is main) -> ...
I want to close all previous activities (Welcome, SignIn, SignUp) when start TabsActivity.
I try several method...
TabsActivity. clear task on launch=true ? but not work (maybe)
TabsActivity. launch mode = singleTask ? but not work
But I do not want to "save 3 activities and call each activity.finish()"
Depending on the situation, "available 2 or 4 activities not 3", or
"I do not know What activities is in activity stack".
I want to clear all previous activities, regadless of any situation.
Help me :)
Sorry my poor english... Thanks.
If I understand correctly, you might want to try starting your TabsActivity with the following code:
Intent intent = new Intent(getApplicationContext(), TabsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
The flag Intent.FLAG_ACTIVITY_CLEAR_TOP clears the history.
Use
Intent intent = new Intent(getApplicationContext(), ClassToLaunch.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will finish the previous activities
To Close Previous Activities ,you should start New Activity with startActivityForResult and then before finishing the current Activity with finish() call , setResult(value) for previous Activity ,the previous Activity will then get a callback where you can call finish() for the previous Activity.
Clear Backstack of android, from where you are calling tabActivity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Close all activities of my application

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();

Categories

Resources