尝试使用finish(),finishAfinity()和意图标记清除任务。
//活动A
public class A{
public void finishActivity(){
finish();
}
}
//活动B
public class B{
Activity a = new ActivityA();
a.finishActivity();
}
我希望活动B被活动B关闭
答案 0 :(得分:0)
您可以使用广播接收器从活动B关闭活动A。使用对象从B调用活动A将导致空指针异常。这是因为,当活动B启动时,将调用活动A的onPause()方法。
Use BroadCastReceiver as below
BroadCastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
if (action.equals("finish_activity_a")) {
//Do Something Else
}
}
};
然后按以下步骤注册您的广播接收器:
try {
//Register BroadcastReceiver
registerReceiver(broadcastReceiver, new IntentFilter(BroadCastActions.actionUpdateAppOptions));
} catch (IllegalArgumentException e){
e.printStackTrace();
}
初始化广播接收器后,可以在上面的onCreate方法中或在onStart(),onResume(),onRestart()方法中对其进行注册。
要取消注册广播接收器,请执行以下操作:
try {
//Check If BroadCast Was Received
if (broadcastReceiver != null) {
getActivity().unregisterReceiver(broadCastReceiver);
}
} catch (IllegalArgumentException e){
e.printStackTrace();
}
您可以在onCreate()方法或onStop(),onPause(),onDestroy()方法中取消注册广播接收器。 希望对您有所帮助。请在下面发表评论。祝你好运!