1.使用List集合方式 用list保存activity实例,然后逐一干掉 import java.util.LinkedList; import java.util.Li
1.使用List集合方式
用list保存activity实例,然后逐一干掉
import java.util.LinkedList;
import java.util.List;
import Android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.DialogInterface;
import android.content.Intent;
public class SysApplication extends Application {
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);
}
public void exit() {
try {
for (Activity activity : mList) {
if (activity != null)
activity.finish();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// System.exit(0);//去掉这个
}
}
@Override
public void onLowMemory() {
super.onLowMemory();
System.GC();
}
}
在每个Activity的onCreate方法中添加类似代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SysApplication.getInstance().addActivity(this);
}
在需要退出程序的时候,调用:
SysApplication.getInstance().exit();
简而言之,通过单例模式把每个Activity 的引用添加到一个全局链表中,每次退出程序调用System.exit(0)时,先调用链表中Activity 的finish方法
优点:实现简单,逻辑清晰
2.使用广播
推荐!!好方便!!
基本逻辑就是:定义一个BaseActivity,在这个Activity中绑定广播,在广播的onReceive方法中调用finish();然后以后的Activity都继承这个Activity,退出时发送广播,退出BaseActivity,父activity都退出了,子activity肯定退出。
代码如下:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
public class BaseActivity extends AppCompatActivity {
protected BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
@Override
public void onResume() {
super.onResume();
// 在当前的activity中注册广播
IntentFilter filter = new IntentFilter();
filter.addAction("ExitApp");
this.reGISterReceiver(this.broadcastReceiver, filter);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy(); //解除绑定
this.unregisterReceiver(this.broadcastReceiver);
}
}
一个Activity实例:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ActivityC extends BaseActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_c);
SysApplication.getInstance().addActivity(this);
btn = (Button) findViewById(R.id.acticityc_btn);
//关闭所有的activity
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
private long exitTime = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){//&& event.getAction() == KeyEvent.ACTION_DOWN
if((System.currentTimeMillis()-exitTime) > 2000){
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
myExit();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
protected void myExit() {
//第二种退出方法
// SysApplication.getInstance().exit();
Intent intent = new Intent();
intent.setAction("ExitApp");
this.sendBroadcast(intent);
//super.finish();
}
@Override
protected void onStop() {
super.onStop();
Log.i("tag","ActivityC-onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("tag","ActivityC-onDestroy");
}
}
就是在退出的地方调用这个:
private long exitTime = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){//&& event.getAction() == KeyEvent.ACTION_DOWN
if((System.currentTimeMillis()-exitTime) > 2000){
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
myExit();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
protected void myExit() {
//第二种退出方法
// SysApplication.getInstance().exit();
Intent intent = new Intent();
intent.setAction("ExitApp");
this.sendBroadcast(intent);
//super.finish();
}
优点:方便,方便!!只需要在退出的地方发送广播就可以!!
这是我的退出时的输出:
08-09 15:33:48.869 26919-26919/example.com.closeapp I/tag: MainActivity-onPause
08-09 15:33:49.279 26919-26919/example.com.closeapp I/tag: MainActivity-onStop
08-09 15:33:51.569 26919-26959/example.com.closeapp D/OpenGLRenderer: endAllStagingAnimators on 0x55850598b0 (RippleDrawable) with handle 0x5585041510
08-09 15:33:56.079 26919-26919/example.com.closeapp I/tag: MainActivity-onDestroy
08-09 15:33:56.089 26919-26919/example.com.closeapp I/tag: ActivityB-onDestroy
08-09 15:33:56.399 26919-26919/example.com.closeapp I/tag: ActivityC-onDestroy
以上所述是小编给大家介绍的Android 中 退出多个activity的经典方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程网网站的支持!
您可能感兴趣的文章:Android使用Theme自定义Activity进入退出动画的方法Android仿微信滑动退出Activity退出Android程序时清除所有activity的实现方法Android一次性退出多个Activity的方法android中实现完全退出程序方法(退出所有activity)Android Activity进出动画三种方法Android Activity切换动画详解及实例Android Activity跳转动画效果Android实现Activity界面切换添加动画特效的方法Android的Activity跳转动画各种效果整理Android编程设定activity进入和退出效果的方法
--结束END--
本文标题: Android 中 退出多个activity的经典方法
本文链接: https://lsjlt.com/news/23882.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0