AndroidはアプリケーションのすべてのActivityをオフにします(3)

7403 ワード

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>