Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 中 退出多個activity的經典方法

Android 中 退出多個activity的經典方法

編輯:關於Android編程

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的經典方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved