activity启动FLAG之FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、博主微信(guyun297890152)、QQ技术交流群(183198395)。

从字面含义就能看出来,意思是当TASK重置时,该activity会被清除。运行下面小示例就能看出其作用了.

MainActivity:

package com.home;

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 MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Button btn = new Button(this);
		btn.setText("跳转");
		setContentView(btn);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent in = new Intent(MainActivity.this, SecondActivity.class);
				// 重置该task时清除该activity
				in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
				startActivity(in);
			}
		});
	}

}

SecondActivity:

package com.home;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView tv = new TextView(this);
		tv.setText("the second activity");
		setContentView(tv);
	}
}



 

你可能感兴趣的:(Android,Android之旅)