简体   繁体   English

onCreate(Bundle savedInstanceState)始终为null

[英]onCreate(Bundle savedInstanceState) in always null

I know, this question is asked before on stackoverflow, but non of the answers worked for me. 我知道,这个问题是在stackoverflow上问过的,但是没有一个答案对我有用。

Probably worth mentioning: 大概值得一提:

  • I use ActionBarSherlock with the support package. 我将ActionBarSherlock与支持包一起使用。
  • Method onSaveInstanceState IS called when I press the home button. 当按下主屏幕按钮时,将调用onSaveInstanceState方法。 The method onCreate just always gives NULL for the Bundle savedInstanceState . onCreate方法始终为Bundle savedInstanceState赋予NULL。
  • Method onRestoreInstanceState is never called at all. onRestoreInstanceState不会调用onRestoreInstanceState方法。 (I wouldn't mind if the onCreate worked ;)). (我不介意onCreate有效;)。
  • Also (it shouldn't matter) I tried putting super.onSaveInstanceState(outState) at the bottom of onSaveInstanceState . 另外(没关系),我尝试将super.onSaveInstanceState(outState)放在onSaveInstanceState的底部。 No luck either. 也没有运气。

Here's the code. 这是代码。 I hope someone had this problem and solved it. 我希望有人遇到这个问题并解决了。

public class MainActivity extends SherlockFragmentActivity {

    private static final String LOG_TAG = MainActivity.class.getSimpleName();

    private static String STATE_TO_STORE = "state_to_store";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        

        Log.d(LOG_TAG, "onCreate: savedInstanceState = " + (savedInstanceState == null ? "NULL" : "Not NULL"));

        // ... more code...
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        Log.d(LOG_TAG, "onRestoreInstanceState: savedInstanceState = " + (savedInstanceState == null ? "NULL" : "Not NULL"));
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt(STATE_TO_STORE, 5); // store some int

        Log.d(LOG_TAG, "onSaveInstanceState bundle: " + outState.toString());
    }

    // ... more code ...

}

The logging clearly states onSaveInstanceState is being called and onCreate gets savedInstanceState = NULL . 日志记录清楚地表明正在调用onSaveInstanceState ,并且onCreate获取savedInstanceState = NULL

Check that your manifest does not contain android:noHistory="true". 检查清单是否不包含android:noHistory =“ true”。

I spent hours looking for an answer and it was that simple. 我花了几个小时寻找答案,就是这么简单。

In my case, the reason was that the specific activity did not have a theme declared in the manifest file. 就我而言,原因是特定活动在清单文件中没有声明主题。

To fix this, open AndroidManifest.xml, click Application, select the crashing activity in Application Nodes and add the theme in the Theme field of Attributes. 要解决此问题,请打开AndroidManifest.xml,单击“应用程序”,在“应用程序节点”中选择崩溃的活动,然后在“属性”的“主题”字段中添加主题。 In my case, it was 就我而言,

@style/Theme.AppCompat.Light.DarkActionBar

but you could copy the theme from one of your other activities. 但是您可以从其他活动之一复制主题。

PS: I know this is an answer to an old question, but I've stumbled upon it while searching for a fix and didn't find a working solution so this might help others. PS:我知道这是一个老问题的答案,但是我在寻找修复程序时偶然发现了它,却没有找到可行的解决方案,因此这可能对其他人有所帮助。

onRestoreInstanceState (or saved bundle in onCreate) will be fired when the Activity was killed by the system due to lack of resources and restarted when you get back to it. 当Activity因缺乏资源而被系统杀死时,将触发onRestoreInstanceState(或onCreate中保存的捆绑软件),并在您重新使用它时重新启动。 The Activity might not be killed (just stopped) and restarted without going through onRestoreInstanceState. 在不通过onRestoreInstanceState的情况下,Activity可能不会被杀死(只是停止)并重新启动。 Another words, onSaveInstanceState will be always called, but onRestoreInstanceState will be called if it is killed and restored by the system. 换句话说,将始终调用onSaveInstanceState,但如果系统杀死并恢复了onRestoreInstanceState,则将调用它。 Not just stopped and restarted, not paused and resumed and not started by a new intent. 不仅停止并重新启动,还没有暂停并恢复,也没有通过新的意图启动。

Check my explanation here. 在这里查看我的解释。 I'm sure it covers your question. 我确定它涵盖了您的问题。

when is onRestoreInstanceState called? 何时调用onRestoreInstanceState?

When you press the HOME button, your activity is pause and not destroyed Thus when you launch the app again from home screen, onCreate is not called, unless the OS kill your app to reclaim memory. 当您按下HOME按钮时,您的活动将暂停并且不会被破坏。因此,当您从主屏幕再次启动应用程序时,除非操作系统杀死您的应用程序以回收内存,否则不会调用onCreate。 If you want your activity to be recreated when launch from the home screen, put this line android:finishOnTaskLaunch="true" in the activity manifest. 如果希望从主屏幕启动时重新创建活动,请在活动清单中放入以下行android:finishOnTaskLaunch="true"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM