Android关闭应用中的所有Activity(三)

FirstActivity如下:

package cn.testmanageactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 * Demo描述:
 * 在需要的时候关闭应用中的所有Activity
 */
public class FirstActivity extends Activity {
   private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.firstactivity);
		init();
	}
	private void init(){
		mButton=(Button) findViewById(R.id.firstButton);
		mButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
                Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
                startActivityForResult(intent, Util.REQUEST_CODE);
			}
		});
	}
	
	@Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			super.onActivityResult(requestCode, resultCode, data);
			System.out.println("FirstActivity 执行方法onActivityResult()," +
			"此时收到来自SecondActivity消亡时传回的数据:test="+data.getStringExtra("test"));
			finish();
		}

}


SecondActivity如下:

package cn.testmanageactivity;

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

public class SecondActivity extends Activity {
   private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.secondactivity);
		Util.activitiesArrayList.add(this);
		init();
	}
	private void init(){
		mButton=(Button) findViewById(R.id.secondButton);
		mButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
                Intent intent=new Intent(SecondActivity.this,ThirdActivity.class);
                startActivity(intent);
			}
		});
	}

	@Override
	public void finish() {
		Intent intent = new Intent();
		intent.putExtra("test", "hello everyone");
		setResult(111, intent);
		System.out.println("SecondActivity执行覆写后的finish()同时setResult给FirstActivity传值");
		super.finish();
	}

}


ThirdActivity如下:

package cn.testmanageactivity;

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

public class ThirdActivity extends Activity {
   private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thirdactivity);
		Util.activitiesArrayList.add(this);
		init();
	}
	private void init(){
		mButton=(Button) findViewById(R.id.thirdButton);
		mButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
                Intent intent=new Intent(ThirdActivity.this,FourthActivity.class);
                startActivity(intent);
			}
		});
	}
	
	@Override
	public void finish() {
		super.finish();
		System.out.println("ThirdActivity执行覆写后的finish()");
	}

}

 

FourthActivity如下:

package cn.testmanageactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FourthActivity extends Activity {
   private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fourthactivity);
		Util.activitiesArrayList.add(this);
		init();
	}
	private void init(){
		mButton=(Button) findViewById(R.id.fourthButton);
		mButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
			  Util.finishActivities();
			}
		});
	}

	@Override
	public void finish() {
		super.finish();
		System.out.println("FourthActivity执行覆写后的finish()");
	}

}


Util如下:

package cn.testmanageactivity;

import java.util.ArrayList;
import android.app.Activity;

public class Util {
   public final static int REQUEST_CODE=9527;
   public static ArrayList<Activity> activitiesArrayList=new ArrayList<Activity>();
   
	public static void finishActivities() {
		if (activitiesArrayList.size() > 0) {
			Activity activity;
			for (int i = 0; i < activitiesArrayList.size(); i++) {
				activity = activitiesArrayList.get(i);
				if (activity != null) {
					System.out.println("现在finish()掉:" + activity.toString());
					activity.finish();
				}
			}
			activitiesArrayList.clear();
		}
	}
}


firstactivity.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/firstButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到第二个Activity"
        android:layout_centerInParent="true"
        />

</RelativeLayout>


secondactivity.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/secondButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到第三个Activity"
        android:layout_centerInParent="true"
        />

</RelativeLayout>


thirdactivity如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/thirdButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到第四个Activity"
        android:layout_centerInParent="true"
        />

</RelativeLayout>


fourthactivity如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/fourthButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="关闭该Activity"
        android:layout_centerInParent="true"
        />

</RelativeLayout>



 

上一篇:Qt学习之路(6): API文档的使用


下一篇:成功解决Ubuntu下的include/darknet.h:14:14: fatal error: cuda_runtime.h: No such file or directory