[英]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.