首页 > 移动开发 > Android关闭应用中的所有Activity

Android关闭应用中的所有Activity

FirstActivity如下:

[java][/java]

view plaincopy

  1. package cn.testmanageactivity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. /**
  9.  * Demo描述:
  10.  * 在需要的时候关闭应用中的所有Activity
  11.  */
  12. public class FirstActivity extends Activity {
  13.    private Button mButton;
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.firstactivity);
  18.         init();
  19.     }
  20.     private void init(){
  21.         mButton=(Button) findViewById(R.id.firstButton);
  22.         mButton.setOnClickListener(new OnClickListener() {
  23.             @Override
  24.             public void onClick(View view) {
  25.                 Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
  26.                 startActivityForResult(intent, Util.REQUEST_CODE);
  27.             }
  28.         });
  29.     }
  30.     @Override
  31.         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  32.             super.onActivityResult(requestCode, resultCode, data);
  33.             System.out.println("FirstActivity 执行方法onActivityResult()," +
  34.             "此时收到来自SecondActivity消亡时传回的数据:test="+data.getStringExtra("test"));
  35.             finish();
  36.         }
  37. }

SecondActivity如下:

[java][/java]

view plaincopy

  1. package cn.testmanageactivity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. public class SecondActivity extends Activity {
  9.    private Button mButton;
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.secondactivity);
  14.         Util.activitiesArrayList.add(this);
  15.         init();
  16.     }
  17.     private void init(){
  18.         mButton=(Button) findViewById(R.id.secondButton);
  19.         mButton.setOnClickListener(new OnClickListener() {
  20.             @Override
  21.             public void onClick(View view) {
  22.                 Intent intent=new Intent(SecondActivity.this,ThirdActivity.class);
  23.                 startActivity(intent);
  24.             }
  25.         });
  26.     }
  27.     @Override
  28.     public void finish() {
  29.         Intent intent = new Intent();
  30.         intent.putExtra("test", "hello everyone");
  31.         setResult(111, intent);
  32.         System.out.println("SecondActivity执行覆写后的finish()同时setResult给FirstActivity传值");
  33.         super.finish();
  34.     }
  35. }

ThirdActivity如下:

[java][/java]

view plaincopy

  1. package cn.testmanageactivity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. public class ThirdActivity extends Activity {
  9.    private Button mButton;
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.thirdactivity);
  14.         Util.activitiesArrayList.add(this);
  15.         init();
  16.     }
  17.     private void init(){
  18.         mButton=(Button) findViewById(R.id.thirdButton);
  19.         mButton.setOnClickListener(new OnClickListener() {
  20.             @Override
  21.             public void onClick(View view) {
  22.                 Intent intent=new Intent(ThirdActivity.this,FourthActivity.class);
  23.                 startActivity(intent);
  24.             }
  25.         });
  26.     }
  27.     @Override
  28.     public void finish() {
  29.         super.finish();
  30.         System.out.println("ThirdActivity执行覆写后的finish()");
  31.     }
  32. }

FourthActivity如下:

[java][/java]

view plaincopy

  1. package cn.testmanageactivity;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. public class FourthActivity extends Activity {
  8.    private Button mButton;
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.fourthactivity);
  13.         Util.activitiesArrayList.add(this);
  14.         init();
  15.     }
  16.     private void init(){
  17.         mButton=(Button) findViewById(R.id.fourthButton);
  18.         mButton.setOnClickListener(new OnClickListener() {
  19.             @Override
  20.             public void onClick(View view) {
  21.               Util.finishActivities();
  22.             }
  23.         });
  24.     }
  25.     @Override
  26.     public void finish() {
  27.         super.finish();
  28.         System.out.println("FourthActivity执行覆写后的finish()");
  29.     }
  30. }

Util如下:

[java][/java]

view plaincopy

  1. package cn.testmanageactivity;
  2. import java.util.ArrayList;
  3. import android.app.Activity;
  4. public class Util {
  5.    public final static int REQUEST_CODE=9527;
  6.    public static ArrayList<Activity> activitiesArrayList=new ArrayList<Activity>();
  7.     public static void finishActivities() {
  8.         if (activitiesArrayList.size() > 0) {
  9.             Activity activity;
  10.             for (int i = 0; i < activitiesArrayList.size(); i++) {
  11.                 activity = activitiesArrayList.get(i);
  12.                 if (activity != null) {
  13.                     System.out.println("现在finish()掉:" + activity.toString());
  14.                     activity.finish();
  15.                 }
  16.             }
  17.             activitiesArrayList.clear();
  18.         }
  19.     }
  20. }

firstactivity.xml如下:

[html][/html]

view plaincopy

  1.     xmlns:tools="http://schemas.android.com/tools"
  2.     android:layout_width="match_parent"
  3.     android:layout_height="match_parent"
  4.     >
  5.     
  6.         android:id="@+id/firstButton"
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:text="跳转到第二个Activity"
  10.         android:layout_centerInParent="true"
  11.         />

secondactivity.xml如下:

[html][/html]

view plaincopy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     >
  6.     <Button
  7.         android:id="@+id/secondButton"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:text="跳转到第三个Activity"
  11.         android:layout_centerInParent="true"
  12.         />
  13. </RelativeLayout>

thirdactivity如下:

[html][/html]

view plaincopy

  1.     xmlns:tools="http://schemas.android.com/tools"
  2.     android:layout_width="match_parent"
  3.     android:layout_height="match_parent"
  4.     >
  5.     
  6.         android:id="@+id/thirdButton"
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:text="跳转到第四个Activity"
  10.         android:layout_centerInParent="true"
  11.         />

fourthactivity如下:

[html][/html]

view plaincopy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     >
  6.     <Button
  7.         android:id="@+id/fourthButton"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:text="关闭该Activity"
  11.         android:layout_centerInParent="true"
  12.         />
  13. </RelativeLayout>

本文固定链接: http://www.devba.com/index.php/archives/1602.html | 开发吧

报歉!评论已关闭.