我想开发一个android应用程序。我已经使用this教程。 MainActivity代码如下onCreate()工作流混淆
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
if (savedInstanceState == null) {
// withholding the previously created fragment from being created again
// On orientation change, it will prevent fragment recreation
// its necessary to reserve the fragment stack inside each tab
initScreen();
} else {
// restoring the previously created fragment
// and getting the reference
carouselFragment = (CarouselFragment) getSupportFragmentManager().getFragments().get(0);
}
}
据我所知,它检查savedInstanceState线为空或不是,我们正试图保存用户当前的屏幕。我想,它涵盖了按Home按钮并重新打开应用程序并旋转屏幕的可能性。但在目前情况下,应用程序重新打开时不会调用onCreate。但是我通过了以下link的android工作流程,它不接受再次调用onCreate()。但是我们使用savedInstanceState作为参数,用于检查它是否具有以前的状态或不理解。总之,我很困惑savedInstanceState的用法。如果它用于检查以前的状态,那么为什么onCreate不被调用?
编辑:正如评论解释,重新打开应用程序不会导致娱乐。正如我所知onStart()被调用。我如何检查onStart中的savedState重新创建视图或存储以前的片段?
onRestoreInstanceState是解决你困惑的神奇功能 – FlanschiFox
我知道有这样的功能,但我不会使用任何存储的参数。我只是对解释和引用的用法感到困惑。 – starrystar
当新创建活动时,会调用onCreate。当应用程序重新打开时,它会重新启动,而不是重新创建,这意味着您的活动仍然存在。当您旋转手机时,活动将被销毁并在新配置中重新创建,因此onCreate被称为 –