Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing activities stack after a button click

In my app, I start an eight step "wizard" from my landing page in which data is passed from step 1 all the way to step 8. For keeping the data intact while still in between steps, I am not calling finish() on either of the activities. However, when all the steps are complete, is there a way that I can close all the 8 activities I had started and return back to the landing page?

An illustration of sorts here:

Home - Step 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8

At this point, when the user clicks "Save", close all the Steps (8) and go back to the Home Page. I have been creating a new intent to do this so far, but i realize this is not the best solution. A simple back press takes him back to the 7th Step.

Any help appreciated.

like image 270
Siddharth Lele Avatar asked Dec 03 '10 18:12

Siddharth Lele


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 finish current activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do you close an activity on Android?

On your Android phone or tablet, go to myactivity.google.com. Above your activity, tap Delete . Tap All time. Delete.


2 Answers

Intent intent = new Intent(this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

This will kill all the activities in between from your 8th screen and launch your hom screen back. also u can set ur home screen's acitivty in manifest launchmode="singleTop". see this link - developer.android.com/guide/topics/fundamentals.html#acttask

like image 170
Varun Avatar answered Sep 20 '22 18:09

Varun


Another approach would be to use StartActivityForResult(...) to start each activity, and have activities call setResult() before finish(). Then in each Activity's onActivityResult(...) method call finish() if the intent is non-null.

This will create the full stack, and automatically chain-finish them all when the last finishes.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (data == null) {
        return;  // back button, resume this activity
    }

    // Propagate result down the stack.
    setResult(0, data);
    finish();
}

This gives you a little more control and lets the original activity receive the result via onActivityResult rather than the create intent, which might be more intuitive if the original request has other state you want to preserve (in its start intent, in particular).

like image 38
Darrell Avatar answered Sep 22 '22 18:09

Darrell