Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to finish multiple activities on a button click?

I am implementing android application which is on web service. I'm creating login Activity A then homepage Activity B and so on.

Suppose there are A, B, C, D and E etc. activities. I'm creating a home button in Activity E and by clicking on that Activity E, D and C should finish and home Activity B should resume.

How can I implement that?

A > B > C > D > E > back button in E Activity > B.

like image 610
Vishakha Kinjawadekar Avatar asked Mar 04 '11 12:03

Vishakha Kinjawadekar


People also ask

How do I end a button click activity?

Alright the best way to finish the current Activity is by using finish() method. So inside the onClick() of your button in the ActivityTwo you can do this. closeButton. setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); //closes ActivityTwo } });

How do you close all activities?

exit(); or finish(); it exit full application or all activity.

How do I get a button to open another activity in Android Studio?

Using an OnClickListenerInside your Activity instance's onCreate() method you need to first find your Button by it's id using findViewById() and then set an OnClickListener for your button and implement the onClick() method so that it starts your new Activity .


2 Answers

Take a look at the FLAG_ACTIVITY_CLEAR_TOP flag.

In essence it does the following. If the Activity this Intent is directed to is paused in your current back stack then it will resume it and clear the stack above it destroying all the activities in the stack above it.

UPDATE: In response to Jason Hanley.

Thanks for mentioning it. The documentation of FLAG_ACTIVITY_CLEAR_TOP covers that example. If you don't want Activity B to be recreated but just passed the new Intent then you should define its launch mode as singleTop.

like image 192
Octavian A. Damiean Avatar answered Nov 04 '22 20:11

Octavian A. Damiean


Use onActivityResult method in chain . Start closing with the outermost activity-E when pressed on home , then check for its result and accordingly close activity-D and so on .

like image 26
NickHalden Avatar answered Nov 04 '22 21:11

NickHalden