English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When multiple activities are opened in the app, due to the deep entry, many apps have to let users step back to the first interface (MainActivity) step by step, and release them one by one as they step back.
Today, I am writing in Kotlin to implement a method to exit the application directly at any location.
Firstly, whenever an activity is opened, we need a place to keep a record so that a loop can iterate through the code to release all the resources used by the activities.
So, let's create an array. We use MutableList here because it has add and remove methods, which are very convenient.
var activities: MutableList<Activity> = arrayListOf()
This array is used to record how many activities have been opened currently.
So, every time a new activity is opened, we need to perform an action to add it to this array.
fun ActivityAdd(activity: Activity) { activities.add(activity) }
Then, let's write the core operation, which is to release all activities.
Firstly, we need to iterate through the array, and check if the activity we are iterating over is null. If it is not null, it means that the current activity has not been released yet, so we need to finish this activity and remove it from the array.
fun ActivityFinishAll() { for (activity:Activity in activities){ if (activity != null){ activity.finish() activities.remove(activity) } } }
This is not yet finished, the array addition action needs to be executed at the time of each activity's onCreate
ActivityAdd(this)
It is written in override fun onCreate(savedInstanceState: Bundle?) inside the method.
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) ActivityAdd(this) ... }
Of course, this is still not enough, and it is necessary to implement the removal function for each activity separately.
fun ActivityRemove(activity: Activity){ activities.remove(activity) }
This way, it has the basic functions.
PS: OtherActivity method to close
Four methods to end the current activity
//Method one to close the current activity finish(); //Method two to close the current interface android.os.Process.killProcess(android.os.Process.myPid()); //Method three to close the current interface System.exit(0); //Method four to close the current interface this.onDestroy();
But if four activities have already been started: A, B, C, and D,
In Activity D, it is desired to start another Activity B, but not to become A, B, C, D, B, but to hope for A, B, and the data on B should still be retained
Intent intent = new Intent(); intent.setClass(TableActivity.this, FrameActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//Set not to refresh the interface to be jumped to intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//它可以关掉所要到的界面中间的activity startActivity(intent);
If four activities have already been started: A, B, C, and D,
In Activity D, it is desired to start another Activity B, but not to become A, B, C, D, B, but to hope for A, B, and the data on B should not be retained
Intent intent = new Intent(); intent.setClass(TableActivity.this, FrameActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//它可以关掉所要到的界面中间的activity startActivity(intent);
如果已经启动了四个Activity:A,B,C和D,在D Activity里,想再启动一个 Activity B,但不变成A,B,C,D,B,而是希望是A,C,D,B,则可以像下面写代码:
Intent intent1 = new Intent(TableActivity.this, FrameActivity.class); intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent1);
如果已经启动了四个Activity:A,B,C和D,在D Activity里,想要一次性关闭所有的activity
创建一个专门用来处理activity的类
package com.layout; import java.util.LinkedList; import java.util.List; import android.app.Activity; import android.app.Application; /** * 一个类,用来结束所有后台activity * @author Administrator * */ public class SysApplication extends Application { //运用list来保存每一个activity是关键 private List<Activity> mList = new LinkedList<Activity>(); //为了实现每次使用该类时不创建新的对象而创建的静态对象 private static SysApplication instance; //构造方法 private SysApplication(){} //实例化一次 public synchronized static SysApplication getInstance(){ if (null == instance) { instance = new SysApplication(); } return instance; } // Add Activity public void addActivity(Activity activity) { mList.add(activity); } //Close each activity in the list public void exit() { try { for (Activity activity:mList) { if (activity != null) activity.finish(); } } e.printStackTrace(); } System.exit(0); } } //Kill process public void onLowMemory() { super.onLowMemory(); System.gc(); } }
Add this at the time of creation of each activity
SysApplication.getInstance().addActivity(this);
Add the activity to the list.
Call the exit method of SysApplication when you want to close it
//Close the entire program SysApplication.getInstance().exit();
That's all for the content of this article. Hope it helps everyone's learning and also hope everyone will support the Yelling Tutorial more.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit manually, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)