android click the back button twice to exit the app

First build a management class management stack

public class AppManager {
private static Stack activityStack;
private static AppManager instance;

private AppManager(){}
/**
 * 单一实例
 */
public static AppManager getAppManager(){
    if(instance==null){
        instance=new AppManager();
    }
    return instance;
}
/**
 * 添加Activity到堆栈
 */
public void addActivity(Activity activity){
    if(activityStack==null){
        activityStack=new Stack<Activity>();
    }
    activityStack.add(activity);
}
/**
 * 获取当前Activity(堆栈中最后一个压入的)
 */
public Activity currentActivity(){
    Activity activity=activityStack.lastElement();
    return activity;
}
/**
 * 结束所有Activity
 */
public void finishAllActivity(){
    for (int i = 0, size = activityStack.size(); i < size; i++){
        if (null != activityStack.get(i)){
            activityStack.get(i).finish();
        }
    }
    activityStack.clear();
}

}

The second is to create a BaseActivity to write some public methods, etc. Here we only operate to add the activity to the stack and manage
public class BaseActivity extends Activity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //将Activity实例添加到AppManager的堆栈
    AppManager.getAppManager().addActivity(this);
}
@Override
protected void onDestroy() {
    super.onDestroy();
    //将Activity实例从AppManager的堆栈中移除
    AppManager.getAppManager().finishActivity(this);
}
long firstTime=0;
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if(keyCode==KeyEvent.KEYCODE_BACK){     //KEYCODE_BACK:回退键
        long secondTime= System.currentTimeMillis();
        if (secondTime-firstTime>2000){
            Toast.makeText(AppManager.getAppManager().currentActivity(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            firstTime=System.currentTimeMillis();
            return true;
        }else{
            AppManager.getAppManager().finishAllActivity();
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            this.startActivity(intent);
            System.exit(0);

        }
    }
    return super.onKeyUp(keyCode, event);
}

}

Then rewrite the onkeydown method in this method to execute the method of exiting the program to define a time record to judge the click interval and then prompt the customer whether to exit the application, etc.
AppManager.getAppManager().finishAllActivity(); This method can be written or not, depending on your needs If you need to record stack records, you can add it if you don't need to enter your mainactivity directly.
Let all activities inherit BaseActivity. Listen here. You can listen to all activities. You can exit the program anywhere.

Of course, you can also perform monitoring and rewrite the onkeydown method in the activity you need.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325594383&siteId=291194637