监听返回键并退出所有Activity示例
来源:互联网 发布:ubuntu查看组的用户 编辑:程序博客网 时间:2024/05/16 07:01
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>
0 0
- 监听返回键并退出所有Activity示例
- 监听返回键并退出所有Activity示例
- 监听返回键并退出所有Activity示例
- 退出登录并finish所有的activity
- activity中监听返回键
- android activity返回键监听
- quick-cocos2d-x android返回键监听并实现原生退出对话框
- 修改密码后退回到登录界面,再按返回键退出应用,关闭所有的Activity
- 主界面监听返回键,退出程序
- Unity 监听键盘 返回键退出程序
- 监听返回键、点击两次退出程序
- 安卓返回键监听:退出程序
- 在某个Activity上点击返回时,退出全部的Activity并退出应用程序
- 一键停止所有activity,退出程序
- 实现随时随地一键退出所有Activity
- 实现随时随地一键退出所有Activity
- 怎样一键退出所有activity
- android退出所有Activity
- C#Hashtable键值对集合
- JQuery的父、子、兄弟节点查找方法
- coredata 查找,增,删,改
- 深圳郎仁科技iobd2利用代码
- HI258摄像头旋转配置问题
- 监听返回键并退出所有Activity示例
- OJ 系列之【中级】双链表基本操作
- cc2530-基于contiki系统读取DHT11问题总结
- not allowed to access to crontab because of pam configuration
- Ubuntu android studio 创建虚拟设备问题
- sqlserver删除所有表(表结构和数据)
- java 常见的2种单例模式
- Vert.x 3学习笔记---06
- PowerDesigner使用教程