WISHY
WISHY

Reputation: 11999

Finish an activity in 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.

Upvotes: 0

Views: 116

Answers (6)

learner
learner

Reputation: 3110

Intent i = new Intent(Activity3.this,HomeActivity.class);
startActivity(i);
HomeActivity.this.finish();

Upvotes: 0

Cheerag
Cheerag

Reputation: 435

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

Upvotes: 1

Vaibhav Agarwal
Vaibhav Agarwal

Reputation: 4499

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.

Upvotes: 3

SMR
SMR

Reputation: 6736

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.

Upvotes: 1

Shaji Thorn Blue
Shaji Thorn Blue

Reputation: 551

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

Upvotes: 1

Waqar Ahmed
Waqar Ahmed

Reputation: 5068

call finish on the first home activity as well. i.e when you start activity 'Activity 1' after startactivity() call finish also.

Upvotes: 0

Related Questions