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

BaseActivity如下:

package cn.testmanageactivity;

import android.app.Activity;
import android.os.Bundle;
/**
 * Demo描述:
 * 通过继承BaseActivity,在某个Activity中按下Back
 * 键时关闭所有Activity,从而退出应用
 * 主要方法:
 * 在BaseActivity的onRestart()生命周期方法中
 * 调用finish().
 */
public class BaseActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		finish();
	}
}


FirstActivity如下:

package cn.testmanageactivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FirstActivity extends BaseActivity {
   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, 9527);
			}
		});
	}
	
	@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"));
		}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.out.println("onDestroy()--->"+this.toString());
	}

}


SecondActivity如下:

package cn.testmanageactivity;
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 BaseActivity {
   private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.secondactivity);
		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);
			}
		});
	}
	/**
	 * 注意:
	 * 在此要先完成业务操作
	 * 再调用super.onRestart();
	 * 若先调用super.onRestart();
	 * 那么就直接finish了,业务逻辑
	 * 也就无法执行
	 */
	@Override
	protected void onRestart() {
		Intent intent = new Intent();
		intent.putExtra("test", "hello everyone");
		setResult(111, intent);
		System.out.println("SecondActivity执行覆写后的finish()同时setResult给FirstActivity传值");
		System.out.println("onDestroy()--->"+this.toString());
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.out.println("onDestroy()--->"+this.toString());
	}

}


ThirdActivity如下:

package cn.testmanageactivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ThirdActivity extends BaseActivity {
   private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thirdactivity);
		init();
	}
	private void init(){
		mButton=(Button) findViewById(R.id.thirdButton);
		mButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
                finish();
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.out.println("onDestroy()--->"+this.toString());
	}

}


firstactivity如下:

<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如下:

<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>



 

上一篇:DBLINK的说明


下一篇:Spring Boot 2.X(十九):集成 mybatis-plus 高效开发