Hi,

Maybe you had some problem which i can discribe and show the way to fix it.

Actually, problem is a closing all your Android application’s Activities through simple call.

There are many ways to fix this. I want to show you one of them.

Before starting i want to ask you something. Please email me or post a comment for this post if you find better and more flexible way to close all Android application’s activities. I will be appreciative you!!

Below you can download the sample project. This project contains 3 (three) activities and ONE abstract Activity. All Activities in this sample project extended this abstract Activity class. This Activity extended Activity class from Android API.

The most interesting contains in this abstract Activity. Here the source:

package com.hrupin;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public abstract class AppBaseActivity extends Activity {
	public static final String FINISH_ALL_ACTIVITIES_ACTIVITY_ACTION = "com.hrupin.FINISH_ALL_ACTIVITIES_ACTIVITY_ACTION";
	private BaseActivityReceiver baseActivityReceiver = new BaseActivityReceiver();
	public static final IntentFilter INTENT_FILTER = createIntentFilter();

	private static IntentFilter createIntentFilter() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(FINISH_ALL_ACTIVITIES_ACTIVITY_ACTION);
		return filter;
	}

	protected void registerBaseActivityReceiver() {
		registerReceiver(baseActivityReceiver, INTENT_FILTER);
	}

	protected void unRegisterBaseActivityReceiver() {
		unregisterReceiver(baseActivityReceiver);
	}

	public class BaseActivityReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(FINISH_ALL_ACTIVITIES_ACTIVITY_ACTION)) {
				finish();
			}
		}
	}

	protected void closeAllActivities() {
		sendBroadcast(new Intent(FINISH_ALL_ACTIVITIES_ACTIVITY_ACTION));
	}
}

You can see that this class contains BrodcastReceiver –> BaseActivityReceiver

This broadcastReceiver makes finish() Activity. This class also contains methods to create intentFilter, register/unregister broadcastReceiver and method closeAllActivities() which broadcast Intent to close Activities of only YOUR Android apllication.

The code is pretty simple. 🙂
Here you can download the sample project

Download it from github

14 Comments

karensilkwood · 1 March, 2012 at 17:48

This is just gorgeous code!! Fitted nicely into my ActivityBase! 🙂

marek · 16 April, 2012 at 21:59

thanks for the code 🙂

Koti · 9 June, 2012 at 14:57

Nice,My issue solved thank you

Cyol · 25 July, 2012 at 21:36

At last after a lot of searching for finish all my activities, it’s simple and elegant !

Thank you

Cyol · 25 July, 2012 at 21:39

Can i Translate this in french on my blog ? (with a link to here and your name of course)

Igor · 27 July, 2012 at 21:32

Hello Cyol,
Course you can.

mano · 29 January, 2014 at 07:38

Application closed caused by-illegalArgumentException:Receiver not registered(unRegisteredBaseActivityReceiver)

mano · 29 January, 2014 at 08:17

Thanks for your code dude:) Now My Application working good…

varshit · 8 November, 2014 at 08:43

Thank you very much 🙂
This example is very helpful for me

Thanks One again 🙂

varshit · 8 November, 2014 at 08:44

Thank you very much 🙂
This example is very helpful for me

Thanks One again 🙂

amier · 20 November, 2014 at 21:44

thank you very much 🙂
your code is very helpful for me. 🙂

Kesava Reddy · 15 March, 2016 at 06:56

Thank You very much.

The code is awesome..!

Peter Phyo · 24 March, 2017 at 07:07

Thanks

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.