当然,此页面上有更好的答案,但作为一种解决方法,您可以使用 SharedPreferences 将消息传递给 Activity A,请求它也完成。
活动一:
public class A extends Activity {
public static final String CLOSE_A_ON_RESUME = "CLOSE_A_ON_RESUME";
@Override
public void onResume(){
super.onResume();
//Retrieve the message
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean IShouldClose=mPrefs.getBoolean(A.CLOSE_A_ON_RESUME,false);
if (IShouldClose){
//remove the message (will always close here otherwise)
mPrefs.edit().remove(A.CLOSE_A_ON_RESUME).commit();
//Terminate A
finish();
}
}
活动 C:
public class C extends Activity {
/*
* Stores an application wide private message to request that A closes on resume
* call this in your button click handler
*/
private void finishCthenA(){
//Store the message
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.edit().putBoolean(A.CLOSE_A_ON_RESUME,true).commit();
//finish C
finish();
}
请注意,这有点冒险,因为首选项在重新启动后仍然存在,并且它可以阻止 A 启动,例如,如果您的应用程序在 A 恢复之前被终止。要解决此问题,您还应该删除 A.onCreate() 中的消息