在做项目的时候,很多时候我们会因为需求的原因去销毁不在栈顶中的Activity,实现不同的跳转逻辑。今天我们就一起来学习一下如何用自定义的广播来实现销毁处于栈中但不是栈顶的Activity。首先我先新建一个项目,第一个activity启动之后里面有一个按钮,点击后回跳转到第二个activity。此时第一个activity会处于栈中,第二个activity会处于栈顶。在第二个activity里面动态注册一个自定义的广播用于控制第二个activity的销毁。然后第二个activity跳转到第三个activity。此时第二个activity也会处于栈中,第三个activity会处于栈顶。在第三个activity发送对应的第二个activity注册的广播事件,这时候就能控制第二个activity销毁。点击手机的返回键在正常情况下是第三个activity会被销毁然后跳转到第二个activity。但当第三个activity发送了对应的广播后因为第二个activity已经被销毁了,所以点击返回键直接会跳转到第一个activity。下面就是对应的实现代码。
BaseActivity.java
public abstract class BaseActivity extends Activity implements View.OnClickListener {
public Button mButton;
public Context mContext;
public TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
mContext = this;
initView();//初始化View
baseOnCreate();
}
//初始化View
private void initView() {
mButton = (Button) findViewById(R.id.btn_goto_next);
mTextView = (TextView) findViewById(R.id.tv_text);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_goto_next:
buttonClickEvent();
break;
}
}
public abstract void baseOnCreate();
public abstract void buttonClickEvent();
}
FirstActivity.java
public class FirstActivity extends BaseActivity {
@Override
public void baseOnCreate() {
mButton.setText("Go to SecondActivity");
mTextView.setText("Hello I am FirstActivity");
mButton.setBackgroundColor(getResources().getColor(R.color.red));
mTextView.setTextColor(getResources().getColor(R.color.red));
}
@Override
public void buttonClickEvent() {
Intent intent = new Intent(mContext, SecondActivity.class);
startActivity(intent);
}
}
SecondActivity.java
public class SecondActivity extends BaseActivity {
private FinshReceiver mFinsh;
@Override
public void baseOnCreate() {
mFinsh = new FinshReceiver();
registerReceiver(mFinsh, new IntentFilter("FinishActivity"));//动态注册一个自定义的广播,Action为FinishActivity
mButton.setText("Go to ThirdActivity");
mTextView.setText("Hello I am SecondActivity");
mButton.setBackgroundColor(getResources().getColor(R.color.green));
mTextView.setTextColor(getResources().getColor(R.color.green));
}
@Override
public void buttonClickEvent() {
Intent intent = new Intent(mContext, ThirdActivity.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mFinsh);//注销动态注册的广播
}
//广播接收事件
private class FinshReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
}
}
ThirdActivity.java
public class ThirdActivity extends BaseActivity {
@Override
public void baseOnCreate() {
mButton.setText("Finish SecondActivity");
mTextView.setText("Hello I am ThirdActivity");
mButton.setBackgroundColor(getResources().getColor(R.color.blue));
mTextView.setTextColor(getResources().getColor(R.color.blue));
}
@Override
public void buttonClickEvent() {
Intent intent = new Intent("FinishActivity");
sendBroadcast(intent);//发送对应的广播
}
}
附:源码下载地址