Android完全退出应用程序

时间:2021-07-17 20:32:12

转载请标明出处:http://blog.csdn.net/wu_wxc/article/details/52434568
本文出自【吴孝城的CSDN博客】

在Android的Activity中,当有多个Activity多次跳转后,点击返回或退出按钮会发现它会出现循环,会将刚刚打开的Activity,打开了多少次,一次次返回回去,当回到最开始的Activity后才会回到桌面

想要应用程序完全退出,
可定义一个栈,利用单列模式管理Activity,写一个ExitApplication类。
在每个Activity的onCreate()方法中调用ExitApplication.getInstance().addActivity(this);方法,将该Activity添加到ExitApplication实例中,
在要退出的地方调用ExitApplication.getInstance().exit();方法,从而将整个应用程序完全退出。

下面是一个列子,有两个Activity,再写一个ExitApplication类。布局代码就不贴了
先写ExitApplication类
ExitApplication.java

package cn.wuxiaocheng.myapp;

import android.app.Activity;
import android.app.Application;

import java.util.LinkedList;
import java.util.List;

public class ExitApplication extends Application {
private List<Activity> activityList = new LinkedList<Activity>();
private static ExitApplication instance;

private ExitApplication() {

}

// 单例模式中获取唯一的ExitApplication实例
public static ExitApplication getInstance() {
if (null == instance) {
instance = new ExitApplication();
}
return instance;
}

// 将Activity添加到容器中
public void addActivity(Activity activity) {
activityList.add(activity);
}

// 当要退出Activity时,遍历所有Activity 并finish
public void exit() {
for (Activity activity : activityList) {
activity.finish();
}
System.exit(0);
}
}

MainActivity.java

package cn.wuxiaocheng.myapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//将该Activity添加到ExitApplication实例中,
ExitApplication.getInstance().addActivity(this);
}

// Button按钮,跳转到其他Activity
public void click(View v) {
Intent intent = new Intent(MainActivity.this, Test.class);
startActivity(intent);
}

// 物理返回键,双击退出
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(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
ExitApplication.getInstance().exit();
}
return true;
}
return super.onKeyDown(keyCode, event);
}


// 退出按钮
public void back(View v) {
// 结束所有Activity
ExitApplication.getInstance().exit();
}
}

Test.java

package cn.wuxiaocheng.myapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;


public class Test extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}

// Button按钮,跳转到其他Activity
public void onc(View v) {
Intent intent = new Intent(Test.this, MainActivity.class);
startActivity(intent);
Test.this.finish();
}

}