zoukankan      html  css  js  c++  java
  • Android 随时随地的退出程序

    1.建立一个ActivityCollector来管理所有的Activity

    public class ActivityCollector {
        public static List<Activity> activities = new ArrayList<Activity>();
        public static void addActivity(Activity activity){
            activities.add(activity);
        }
        public static void removeActivity(Activity activity){
            activities.remove(activity);
        }
        public static void finishAll(){
            for (Activity activity: activities) {
                if (!activity.isFinishing()){
                    activity.finish();
                }
            }
        }
    };

    新建一个BaseActivity继承Activity,调用控制器的方法

    public class BaseActivity  extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            Log.d("BaseActivity", getClass().getSimpleName());
            ActivityCollector.addActivity(this);
        }
        @Override
        public void onDestroy(){
            super.onDestroy();
            ActivityCollector.removeActivity(this);
        }
    }

    让所有的Activity都继承这个基类,并绑定按钮调用ActivityCollector.finishAll()方法

    比如在Main2Activity里

    public class Main2Activity extends BaseActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d("ThirdActivity", "Task id is " + getTaskId());
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main2);
            Button button3 = (Button) findViewById(R.id.button3);
            button3.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    ActivityCollector.finishAll();
                    //android.os.Process.killProcess(android.os.Process.myPid());
                }
            });
        }

    这样,我们在第二个Activity中点击Button3,便可以退出所有的Activity.

  • 相关阅读:
    ASP.NET Boilerplate
    Financial.IPmt/Financial.PPmt
    VB内部函数(三)——财务函数
    Convert VB.NET to C#
    MySQL 使用自增ID主键和UUID 作为主键的优劣比较详细过程(从百万到千万表记录测试)
    Oauth2.0客户端服务端示例
    一张图搞定OAuth2.0
    使用JAVA实现的一个简单IOC注入实例
    谈谈对Spring IOC的理解
    秒懂,Java 注解 (Annotation)你可以这样学
  • 原文地址:https://www.cnblogs.com/javacoding/p/5550443.html
Copyright © 2011-2022 走看看