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
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.
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.
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. 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? 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 ...
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.
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();
}
});
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()
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With