Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to finish multiple activities at once in android?

I have my Activity flows like this ACTIVITY A -> ACTIVITY B -> ACTIVITY C -> ACTIVITY D. When the user is on Activity D and click a button called exit, the application should go back to Activity B and finish the Activities C and D. How can I do that?

NOTE : ACTIVITY B and ACTIVITY D are the same class but different instance

like image 711
Earwin delos Santos Avatar asked Aug 25 '15 07:08

Earwin delos Santos


People also ask

How do you finish multiple activities?

When the user is on Activity D and click a button called exit, the application should go back to Activity B and finish the Activities C and D.

What is multiple activity in Android?

Even the simplest applications have more than one functionality. Hence, there is often a need to deal with multiple activities. For example, a game can have two activities: a high scores screen and a game screen.

How to work with multiple activities in Android Studio?

Here, you are successfully working with the multiple activities. Navigate the created and executed activities in and an Android Studio Application. Click the second activity button and it will show the second activity page. Click the third activity button and it will show the third activity page.

How to switch between activities in Android?

How to Switch between Activities in Android. Create a New Android Project. First, open a new project with Blank Activity. And give it a name as you want (say FirstActivity). Open the layout file for this Activity. Here we are going to make a Button and an EditText, and on Button click, we will navigate to another Activity.

How to close all activities at once in Android?

How to close all activities at once in android? Android Apps/Applications Mobile Development. This example demonstrates how do I close all activities at once in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code ...

How to view the activity page of an Android Studio application?

Navigate the created and executed activities in and an Android Studio Application. Click the second activity button and it will show the second activity page. Click the third activity button and it will show the third activity page. If you have any doubts, just comment below.


2 Answers

In AndroidManifest.xml, set android:noHistory as true for Activity B, C and D. set it as false for Activity A (actually, the default is false).

Demo:

<activity android:name=".xx.xx.ActivityA" 
    android:noHistory="false"
    android:screenOrientation="nosensor">
</activity>

<activity android:name=".xx.xx.ActivityB" 
    android:noHistory="true"
    android:screenOrientation="nosensor">
</activity>        

<activity android:name=".xx.xx.ActivityC" 
    android:noHistory="true"
    android:screenOrientation="nosensor">
</activity>

<activity android:name=".xx.xx.ActivityD" 
    android:noHistory="true"
    android:screenOrientation="nosensor">
</activity>

For the exit Button:

exitBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getApplicationContext(),ActivityB.class);
        startActivity(intent);
        finish();
    }
});
like image 131
Vigor Avatar answered Nov 15 '22 05:11

Vigor


Quite complicated solution, but allows to fully manipulate backstack. Basically create your own "backstack" in you Application:

public class MyApplication extends Application {

    private Set<Activity> runningActivities = new HashSet<>();

    onCreate() {
        // ...
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                runningActivities.add(activity);
            }
            @Override public void onActivityDestroyed(Activity activity) {
                runningActivities.remove(activity);
            }
        });
    }

    public void killActivities(Set<Class<?>> activitiesToRemove) {
        for (Activity stackActivity : runningActivities) {
            if (activitiesToRemove.contains(stackActivity.getClass())) {
                stackActivity.finish();
            }
        }
    }
}

Now you can call something like this in you ActivityD:

((MyApplication)getApplication()).killActivities(ActivityD.class, ActivityC.class)

You can use Map instead of Set if class isn't only specifier necessary to determine which activity should be removed...

Please corret me if there is something wrong with my solution, but it seems like there should be no leaks and no boilerplate in your activities

Edit - Kotlin version:

class MyApplication : Application() {

    private val runningActivities = mutableSetOf<Activity>()

    
    override fun onCreate() {
        // ...
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle) {
                runningActivities.add(activity)
            }
            override fun onActivityDestroyed(activity: Activity) {
                runningActivities.remove(activity)
            }
        })
    }

    fun killActivities(activitiesToRemove: Set<Class<Activity>>) {
        runningActivities.forEach { activity ->
            if (activitiesToRemove.contains(activity.javaClass)) {
                activity.finish()
            }
        }
    }
}
like image 22
VizGhar Avatar answered Nov 15 '22 05:11

VizGhar