1. Dalvik VM的本地方法
//杀死进程
android.os.Process.killProcess(android.os.Process.myPid())
//抛异常强制退出
System.exit(0);
2.任务管理器方法
//通过activity管理器重启
ActivityManager activitymanager= (ActivityManager)getSystemService (Context.ACTIVITY_SERVICE);
activitymanager.restartPackage(getPackageName());
注意:需要注册的权限<uses-permission android:name=\"android.permission.RESTART_PACKAGES\"></uses- permission>
3.通过封装到栈里一一结束
//封装界面的容器
List<Activity> activitylist=new LinkedList<Activity>();
//定义我的应用
public static MyApplication instance;
/*实例化我的应用的方法
* 如果我的应用为空的话
* 实例化我的应用
*/
public MyApplication getInstance(){
if(null==instance){
instance=new MyApplication();
}
return instance;
}
/*添加界面的方法
* 直接调用界面添加
*/
public void addActivity(Activity activity){
activitylist.add(activity);
}
/*完全退出我的应用
* 结束每一个界面
* 抛异常强制退出
*/
public void exit(){
for(Activity activity :activitylist){
activity.finish();
}
System.exit(0);
}
4. 根据Activity的声明周期
我们知道Android的窗口类提供了历史栈,我们可以通过stack的原理来巧妙的实现,这里我们在A窗口打开B 窗口时在Intent中直接加入标志 Intent.FLAG_ACTIVITY_CLEAR_TOP,这样开启B时将会清除该进程空间的所有Activity。
在A窗口中使用下面的代码调用B窗口
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //注意本行的FLAG设置
startActivity(intent);
5.自定义一个Actiivty 栈
通过利用一个单例模式的Activity栈来管理所有Activity。并提供退出所有Activity的方法。代码如下:
public class ScreenManager {
private static Stack<Activity> activityStack;
private static ScreenManager instance;
private ScreenManager(){
}
public static ScreenManager getScreenManager(){
if(instance==null){
instance=new ScreenManager();
}
return instance;
}
//退出栈顶Activity
public void popActivity(Activity activity){
if(activity!=null){
activity.finish();
activityStack.remove(activity);
activity=null;
}
}
//获得当前栈顶Activity
public Activity currentActivity(){
Activity activity=activityStack.lastElement();
return activity;
}
//将当前Activity推入栈中
public void pushActivity(Activity activity){
if(activityStack==null){
activityStack=new Stack<Activity>();
}
activityStack.add(activity);
}
//退出栈中所有Activity
public void popAllActivityExceptOne(Class cls){
while(true){
Activity activity=currentActivity();
if(activity==null){
break;
}
if(activity.getClass().equals(cls) ){
break;
}
popActivity(activity);
}
}
}
6.通过封装超类管里栈继承实现
1.
public class ActivityManager {
private Context context;
private static ActivityManager activityManager;
public static ActivityManager getActivityManager(Context context){ if(activityManager == null){
activityManager = new ActivityManager(context); }
return activityManager;
} private ActivityManager(Context context){
this.context = context;
} /** * task map,用于记录activity栈,方便退出程序(这里为了不影响系统回收activity,所以用软引用) */ private final HashMap<String, SoftReference<Activity>> taskMap = new HashMap<String, SoftReference<Activity>>(); /** * 往应用task map加入activity */ public final void putActivity(Activity atv) { taskMap.put(atv.toString(), new SoftReference<Activity>(atv)); } /** * 往应用task map加入activity */ public final void removeActivity(Activity atv) { taskMap.remove(atv.toString()); } /** * 清除应用的task栈,如果程序正常运行这会导致应用退回到桌面 */ public final void exit() {
for (Iterator<Entry<String, SoftReference<Activity>>> iterator = taskMap.entrySet().iterator(); iterator.hasNext();) { SoftReference<Activity> activityReference = iterator.next().getValue(); Activity activity = activityReference.get();
if (activity != null) { activity.finish(); }
} taskMap.clear(); } }
2、创建自己的根activity,重写onCreate与onDestory
public class BaseActivity extends Activity { private ActivityManager manager = ActivityManager.getActivityManager(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); manager.putActivity(this); } @Override protected void onDestroy() { super.onDestroy(); manager.removeActivity(this); } public void exit(){ manager.exit(); } }
3、以后创建的activity都继承这个根activity就可以了,如下:
public class ExitActivity extends BaseActivity implements OnClickListener{ private Button start; private Button exit; private TextView tipTV; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); start = (Button)findViewById(R.id.start_new_activity); exit = (Button)findViewById(R.id.exit_all_activity); tipTV = (TextView)findViewById(R.id.tip_tv); tipTV.setText("activity:"+this.toString()); start.setOnClickListener(this); exit.setOnClickListener(this); } @Override public void onClick(View v) { if(v == start){ Intent intent = new Intent(this , ExitActivity.class); startActivity(intent); }else if(v == exit){ exit(); } } }
转载于:https://www.cnblogs.com/allencoder/p/3643921.html