监听返回键并退出所有Activity示例

  http://blog.csdn.net/u010142437/article/details/12244551
主题 Activity

下面给出3个简单的Activity,并在它们之间跳转,如果想点击某个Activity的返回键时就退出所有Activity,就只需监听该Activity的返回键即可。所有Activity都存放在一个集合里面,退出应用时,只需遍历该集合并逐一finish掉。代码如下:

MyAppalication:存放所有activity,提供添加activity和退出所有activity的方法

package com.home.exitapp;

import java.util.ArrayList;

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

public class MyAppalication extends Application {
  // 存放activity的集合
  private ArrayList<Activity> list = new ArrayList<Activity>();
  private static MyAppalication instance;

  public MyAppalication() {
  }

  /**
   * 利用单例模式获取MyAppalication实例
   * 
   * @return
   */
  public static MyAppalication getInstance() {
    if (null == instance) {
      instance = new MyAppalication();
    }
    return instance;
  }

  /**
   * 添加activity到list集合
   * 
   * @param activity
   */
  public void addActivity(Activity activity) {
    list.add(activity);

  }

  /**
   * 退出集合所有的activity
   */
  public void exit() {
    try {
      for (Activity activity : list) {
        activity.finish();
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      System.exit(0);
    }
  }

  @Override
  public void onLowMemory() {
    super.onLowMemory();
    System.gc();
  }
}

BaseActivity:抽象出添加activity的方法

package com.home.exitapp;

import android.app.Activity;
import android.os.Bundle;

public class BaseActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MyAppalication.getInstance().addActivity(BaseActivity.this);
  }

}

MyActivity1:

package com.home.exitapp;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MyActivity1 extends BaseActivity {
  private Button btn_1_2;
  private Button btn_1_3;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);
    // MyAppalication.getInstance().addActivity(MyActivity1.this);
    btn_1_2 = (Button) findViewById(R.id.main1_btn_1_2);
    btn_1_3 = (Button) findViewById(R.id.main1_btn_1_3);
    btn_1_2.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        Intent intent = new Intent(MyActivity1.this, MyActivity2.class);
        startActivity(intent);
      }
    });

    btn_1_3.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        Intent intent = new Intent(MyActivity1.this, MyActivity3.class);
        startActivity(intent);
      }
    });
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
      // return true;//返回真表示返回键被屏蔽掉
      creatDialog();// 创建弹出的Dialog
    }
    return super.onKeyDown(keyCode, event);
  }

  /**
   * 弹出提示退出对话框
   */
  private void creatDialog() {
    new AlertDialog.Builder(this)
        .setMessage("确定退出app?")
        .setPositiveButton("YES",
            new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog,
                  int which) {
                MyAppalication.getInstance().exit();
              }
            })
        .setNegativeButton("NO", new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
          }
        }).show();
  }
}

MyActivity2:

package com.home.exitapp;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MyActivity2 extends BaseActivity {
  private Button btn_2_1;
  private Button btn_2_3;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    btn_2_1 = (Button) findViewById(R.id.main2_btn_2_1);
    btn_2_3 = (Button) findViewById(R.id.main2_btn_2_3);
    btn_2_1.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        Intent intent = new Intent(MyActivity2.this, MyActivity1.class);
        startActivity(intent);
      }
    });

    btn_2_3.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        Intent intent = new Intent(MyActivity2.this, MyActivity3.class);
        startActivity(intent);
      }
    });
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
      // return true;//返回真表示返回键被屏蔽掉
      creatDialog();// 创建弹出的Dialog
    }
    return super.onKeyDown(keyCode, event);
  }

  /**
   * 弹出提示退出对话框
   */
  private void creatDialog() {
    new AlertDialog.Builder(this)
        .setMessage("确定退出app?")
        .setPositiveButton("YES",
            new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog,
                  int which) {
                MyAppalication.getInstance().exit();
              }
            })
        .setNegativeButton("NO", new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
          }
        }).show();
  }
}

MyActivity3:

package com.home.exitapp;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MyActivity3 extends BaseActivity {
  private Button btn_3_2;
  private Button btn_3_1;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main3);
    btn_3_2 = (Button) findViewById(R.id.main3_btn_3_2);
    btn_3_1 = (Button) findViewById(R.id.main3_btn_3_1);
    btn_3_2.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        Intent intent = new Intent(MyActivity3.this, MyActivity2.class);
        startActivity(intent);
      }
    });

    btn_3_1.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        Intent intent = new Intent(MyActivity3.this, MyActivity1.class);
        startActivity(intent);
      }
    });
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
      // return true;//返回真表示返回键被屏蔽掉
      creatDialog();// 创建弹出的Dialog
    }
    return super.onKeyDown(keyCode, event);
  }

  /**
   * 弹出提示退出对话框
   */
  private void creatDialog() {
    new AlertDialog.Builder(this)
        .setMessage("确定退出app?")
        .setPositiveButton("YES",
            new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog,
                  int which) {
                MyAppalication.getInstance().exit();
              }
            })
        .setNegativeButton("NO", new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
          }
        }).show();
  }
}

main1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I am the first activity"
        android:textSize="25sp" />

    <Button
        android:id="@+id/main1_btn_1_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳到2" />

    <Button
        android:id="@+id/main1_btn_1_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳到3" />

</LinearLayout>

main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f000f0"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I am the second activity"
        android:textSize="25sp" />

    <Button
        android:id="@+id/main2_btn_2_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳到1" />

    <Button
        android:id="@+id/main2_btn_2_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳到3" />

</LinearLayout>

main3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00f0ff"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I am the third activity"
        android:textSize="25sp" />

    <Button
        android:id="@+id/main3_btn_3_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳到2" />

    <Button
        android:id="@+id/main3_btn_3_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳到1" />

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值