android退出程序——把activity添加到list,退出时finish()掉所有的activity

 代码——————————————————————

 

package com.example;

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

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

public class Mapplication extends Application {

 private List<Activity> activityList = new LinkedList();
 private static Mapplication instance;

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

    }
    //添加Activity到容器中
    public void addActivity(Activity activity)      {
   activityList.add(activity);
    }
    //遍历所有Activity并finish

    public void exit()
    {

    for(Activity activity:activityList)
    {
   if(activity!=null){
             activity.finish();
            }   }

    System.exit(0);   //关闭JVM

    }
    }

 

package com.example;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import javax.swing.text.html.HTMLDocument;

public class MyActivity extends Activity{
    private  static final String TAG = "MyActivity";
    private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Mapplication.getInstance().addActivity(this);

        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                     Intent intent = new Intent(MyActivity.this,HomeActivity.class);
                     startActivity(intent);
            }
        });

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
                   Mapplication.getInstance().exit();
        }
        return super.onKeyDown(keyCode, event);   


}

 

package com.example;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HomeActivity extends Activity

{
    private Button button;
    private TextView textView;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
             Mapplication.getInstance().addActivity(this);
            textView = (TextView)findViewById(R.id.text);
            textView.setText("HomeActivity");
            button = (Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                         Intent intent = new Intent(HomeActivity.this,TestActivity.class);
                         startActivity(intent);
                }
            });

        }
    }

 

package com.example;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends Activity
{
 private Button button;
    private TextView textView;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
             Mapplication.getInstance().addActivity(this);
            textView = (TextView)findViewById(R.id.text);
            textView.setText("TestActivity");
          button = (Button)findViewById(R.id.button);
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) {
                       Intent intent = new Intent(TestActivity.this,Test2Activity.class);
                       startActivity(intent);
              }
          });

      }
  }

 

package com.example;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Test2Activity extends Activity
{
   private Button button;
    private TextView textView;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
             Mapplication.getInstance().addActivity(this);
            textView = (TextView)findViewById(R.id.text);
            textView.setText("Test2Activity");
           button = (Button)findViewById(R.id.button);
           button.setOnClickListener(new View.OnClickListener() {
               public void onClick(View view) {
                        Intent intent = new Intent(Test2Activity.this,MyActivity.class);
                        startActivity(intent);
               }
           });

       }
   }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值