【问题标题】:Android: on screen rotation what should Fragment's onCreate() and onCreateView() doAndroid:在屏幕旋转上 Fragment 的 onCreate() 和 onCreateView() 应该做什么
【发布时间】:2015-12-17 19:35:50
【问题描述】:

在我的活动中,我检查了 savedInstanceState,确保我没有创建多个片段

但我的问题是我是否应该在 Fragment 的 onCreate() 和 onCreateView() 中进行类似的检查

因为当我旋转屏幕时,Fragment 的 onCreate() 和 onCreateView() 每次都会被调用。

问题是,这两种方法是否可以在每次屏幕旋转后重新执行那里的工作,或者它们也应该进行 savedInstanceState 检查。

现在我的 onCreate() 进行服务调用并 onCreateView 膨胀视图 (Recyclerview)

【问题讨论】:

    标签: android android-fragments android-lifecycle screen-rotation


    【解决方案1】:

    当一个activity或Fragment被重新创建时,首先会触发onCreate()方法,然后是onRestoreInstanceState()方法,这使你可以通过它的Bundle对象来检索你之前在onSaveInstanceState()方法中保存的状态。论据:

    @ Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    //---retrieve the information persisted earlier---
    String ID = savedInstanceState.getString(“ID”);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      相关资源
      最近更新 更多