简体   繁体   English

Android:屏幕旋转时,Fragment的onCreate()和onCreateView()应该做什么

[英]Android: on screen rotation what should Fragment's onCreate() and onCreateView() do

In my activity I have a check for savedInstanceState, making sure I am not creating multiple fragments 在我的活动中,我检查了一下saveInstanceState,确保没有创建多个片段

But my question is should I have similar checks in Fragment's onCreate() and onCreateView() 但是我的问题是我是否应该在Fragment的onCreate()和onCreateView()中进行类似的检查

Because when I rotate screen Fragment's onCreate() and onCreateView() are called everytime. 因为当我旋转屏幕时,Fragment的onCreate()和onCreateView()每次都被调用。

Question is, Is it OK for these 2 methods to re-do there job after everyscreen rotation or they should have a savedInstanceState check as well. 问题是,这两种方法在每次屏幕旋转后是否都可以重新执行工作,还是应该也进行一次saveInstanceState检查?

Right now my onCreate() makes a service call and onCreateView inflates a view (Recyclerview) 现在,我的onCreate()进行服务调用,并且onCreateView扩大视图(Recyclerview)

When an activity or Fragment is recreated, the onCreate() method is first fired, followed by the onRestoreInstanceState() method, which enables you to retrieve the state that you savedpreviously in the onSaveInstanceState() method through the Bundle object in its argument: 重新创建活动或片段时,将首先触发onCreate()方法,然后触发onRestoreInstanceStateState()方法,该方法使您能够通过其对象中的Bundle对象检索以前保存在onSaveInstanceState()方法中的状态:

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

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

相关问题 Fragment 的 onCreate 和 onCreateView 没有被调用 - Fragment's onCreate and onCreateView are not called Android-片段onCreate()和onCreateView()中捆绑的saveInstanceState为null - Android - Bundle savedInstanceState is null in Fragment onCreate() and onCreateView() 在片段onCreateView方法中该做什么? - What to do in the Fragment onCreateView Method? 在活动的onCreate之前调用片段的onCreateView - Fragment's onCreateView called before activity's onCreate Fragment中的onCreate()和onCreateView()生命周期方法有什么不同? - what is the different between onCreate() and onCreateView() lifecycle methods in Fragment? 为什么调用片段onCreateView,onCreate,onActivityCreated - Why does the fragment's onCreateView, onCreate, onActivityCreated are called 在完成onCreate中的活动时,我的片段onCreateView在onCreate之前是如何调用的? - How is my fragment's onCreateView called before it's onCreate when finishing the activity in onCreate? Android Fragment onCreateView() 方法中的容器是什么 - Android Fragment what container is it in onCreateView() method 当Fragment的onCreateView调用时,Android OutOfMemoryError - Android OutOfMemoryError when Fragment's onCreateView called 在Android中停止onCreate在屏幕旋转时执行 - Stopping onCreate being executed on screen rotation in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM