Get Current Activity and Base Activity Class
Create a Java base class named BaseActivity to inherit from
android.support.v7.app.AppCompatActivity
Then override the onCreate method inside this base class
Add a log statement within it
Log.d(“Current Activity”,getClass().getSimpleName());
With this setup, no matter which activity we navigate into, we can know which one is active
package com.foxdevelop.www.lifedemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Current Activity",getClass().getSimpleName());
}
}Code language: JavaScript (javascript)
05-13 08:36:47.473 18499-18499/? D/Current Activity: MainActivity
05-13 08:36:56.460 18499-18499/com.foxdevelop.www.lifedemo D/Current Activity: NormalActivity
05-13 08:37:03.839 18499-18499/com.foxdevelop.www.lifedemo D/Current Activity: Activity3
05-13 08:38:11.104 18499-18499/com.foxdevelop.www.lifedemo D/Current Activity: NormalActivity
One-Click App Exit
If we are on the third activity and want to exit the app, we need to press back three times. This is inconvenient
Pressing Home does not exit the app; it only suspends it.
So how do we implement one-click app exit?
We can create a dedicated collection class to manage all activities.
Add an activity manager to handle this task
This is a collection class containing a list to store all activities. It provides add and remove methods to add or delete single activities in the list
Then inside the base Activity, call add in onCreate and remove in onDestroy
This ensures the list only keeps activities currently on the back stack
Because the list holds references to each activity, calling finish() on them works directly on those activity instances.
Then add a finishAll method to the collection class
In this method, use a foreach loop and call finish() for each activity respectively
Let’s test this below
Register a button click event on the third activity and invoke this collection class.
Wrap finishAll as a static method.
To make sure the app exits fully, we can use the system killProcess method to terminate the current app process.
android.os.Process.KillProcess(android.os.Process.myPid());
Activity Manager Class
package com.foxdevelop.www.lifedemo;
import android.app.Activity;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
public class ActivityHelper {
public static List<Activity> activities = new ArrayList<Activity>();
public static void add (Activity a)
{
activities.add(a);
}
public static void remove (Activity a)
{
activities.remove(a);
}
public static void finishAll()
{
for (Activity a:activities)
{
a.finish();
}
}
}Code language: JavaScript (javascript)
Add and Remove Activities in Base Activity
Add activity when it is created: onCreate
Remove activity when it gets destroyed: onDestroy
package com.foxdevelop.www.lifedemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Current Activity",getClass().getSimpleName());
ActivityHelper.add(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityHelper.remove(this);
}
}Code language: JavaScript (javascript)
Add Button in Activity3 to Call finishAll Method
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third Activity" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exit App" />
</LinearLayout>Code language: HTML, XML (xml)
public class Activity3 extends BaseActivity {
private static final String TAG = "Third Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3);
Log.e(TAG, "" + getTaskId());
Button b = (Button)findViewById(R.id.button);
b.setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View view){
ActivityHelper.finishAll();
}
}
);
}
}Code language: PHP (php)
Operations for Activities