在下面的代码中,我从onPause调用isFinishing(),因此,如果活动完成,我将调用finish()方法,从而调用shutDownDatabase()模块。 在测试代码期间,我遇到了isFinishing()为true的情况,随后调用了shutdownDownDatabase()。但是,也调用了onStop(),这导致了shutdownDownDatabase()模块 再次被调用。
请说明一下尽管活动正在进行中,为什么onStop()被调用了吗?因为我希望在onPause()中的活动时,永远不会像isFinishing()那样调用onStop()。
请查看下面发布的代码和日志。
代码:
@Override
protected void onPause() {
super.onPause();
Log.w(TAG_LOG, "->onPause");
if(isFinishing()) {
this.finish();
}
}
@Override
public void finish() {
String TAG_LOG = ActMain.TAG_LOG + "->" + "finish";
super.finish();
Log.w(TAG_LOG, "->Activity is finishing");
this.shutDownDatabase();
}
private void shutDownDatabase() {
String TAG_LOG = ActMain.TAG_LOG + "->" + "shutDownDatabase";
Log.w(TAG_LOG, "is called");
OptionalsUtils.toOptional(this.getBuiltMovieRoomPersistentDatabase())
.ifPresent(x->{
Log.w(TAG_LOG, "->Movie Room Persistent Database will shutdown.");
this.setIsDatabaseOpen(false);
x.close();
});
}
@Override
protected void onStop() {
super.onStop();
String TAG_LOG = ActMain.TAG_LOG + "->" + "onStop";
Log.w(TAG_LOG, "is called.");
this.shutDownDatabase();
}
@Override
protected void onDestroy() {
super.onDestroy();
String TAG_LOG = ActMain.TAG_LOG + "->" + "onDestroy";
Log.w(TAG_LOG, "->is called.");
}
logcat :
2018-12-16 12:26:10.989 com.example.pc_am.room_01 W/ActMain: ->onPause
2018-12-16 12:26:10.996 com.example.pc_am.room_01 W/ActMain->finish: ->Activity is finishing
2018-12-16 12:26:10.997 com.example.pc_am.room_01 W/ActMain->shutDownDatabase: is called
2018-12-16 12:26:10.997 com.example.pc_am.room_01 W/ActMain->shutDownDatabase: ->Movie Room Persistent Database will shutdown.
2018-12-16 12:26:10.997 com.example.pc_am.room_01 V/ActMain: ->setIsDatabaseOpen: false
2018-12-16 12:26:11.838 com.example.pc_am.room_01 W/ActMain->onStop: is called.//<-
2018-12-16 12:26:11.838 com.example.pc_am.room_01 W/ActMain->shutDownDatabase: is called
2018-12-16 12:26:11.838 com.example.pc_am.room_01 W/ActMain->shutDownDatabase: ->Movie Room Persistent Database will shutdown.
2018-12-16 12:26:11.838 com.example.pc_am.room_01 V/ActMain: ->setIsDatabaseOpen: false
2018-12-16 12:26:11.839 com.example.pc_am.room_01 W/ActMain->onDestroy: ->is called.