How to close All activities in your View Stack in android in one click?

By | May 30, 2012

Hello everyone…

Today I will show you how to close all views in android in a single click of a button.
Basically if you open a number of activities, each activity is stacked one above the other like a stack if you are not calling finish() on each activity.

But there is a way to close all these activities at once by clearing the stack.
Here is a simple in which it has two activities and after navigating to the second view, the button click will open the first activity and clear the stack.

CloseAllViews.java – This is the first activity.

package pack.coderzheaven;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CloseAllViewsDemo extends Activity {
	Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b = (Button)findViewById(R.id.Button01);
        b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(CloseAllViewsDemo.this, Second.class));
			}
		});
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="This is the First activity."
    />
<Button
	android:text="Go to Second Activity without closing"
	android:id="@+id/Button01"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</Button>
</LinearLayout>

Second.java – Second Activity.

package pack.coderzheaven;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Second extends Activity {
	Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        b = (Button)findViewById(R.id.Button01);
        b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				 Context context = v.getContext();
		            Intent intent = new Intent(context, CloseAllViewsDemo.class);
		            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// This flag ensures all activities on top of the CloseAllViewsDemo are cleared.
		            context.startActivity(intent);
			}
		});
    }
}

main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="This is the second activity. The button click will clear the stack. click the Backbutton to see."
    />
<Button
	android:text="Go to First Activity without closing"
	android:id="@+id/Button01"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</Button>
</LinearLayout>

The AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CloseAllViewsDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

	<activity android:name=".Second"
                  android:label="@string/app_name" />
    </application>
</manifest>

After coming back to the first activity from second Activity, hit the backbutton to see if any other activities are below. If there is an activity then you will be taken to that activity, otherwise your application will close.

3 thoughts on “How to close All activities in your View Stack in android in one click?

  1. Pingback: Activities Stack Issue

Leave a Reply

Your email address will not be published. Required fields are marked *