[英]Fragment: onCreate being called 4 times
I have a Working model of fragments, when i was debugging the code i saw that the Fragment onCreate is being called 4 times. 我有一个片段的工作模型,当我调试代码时,我看到Fragment onCreate被调用了4次。
Below is my code: MyFragmentActivity 下面是我的代码: MyFragmentActivity
class MyFragmentActivity extends FragmentActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(fragmentID, new MyListFragmentt())
.replace(detailFragmentID, new MyDetailFragment()).commit();
}
}
@Override
protected void onRestart() {
getSupportFragmentManager().beginTransaction().replace(detailFragmentID, new MyDetailFragment()).commitAllowingStateLoss();
}
}
MyDetailFragment.class MyDetailFragment.class
class MyDetailFragment extends Fragment{
// has method like oncreate(),onCreateView(),onSaveInstanceState()
}
How my oncreate of MyDetailFragment is called ? 我的MyDetailFragment的oncreate如何调用? When i go to some other activity and come back and then tilt the device only then oncreate and onSaveInstanceState of MyDetailFragment is called multiple times.
当我进行其他一些活动然后返回然后仅倾斜设备时,会多次调用MyDetailFragment的oncreate和onSaveInstanceState。
How can i solve this, i have looked into few posts on SO but it says that we need use HIDE,Show
methods and other things ? 我如何解决这个问题,我在SO上看了几篇文章,但说我们需要使用
HIDE,Show
方法和其他东西? but What is the proper soultion to this ? 但是正确的选择是什么呢?
EDIT 编辑
When i am coming back from previous activity, my data in the MyDetailFragment needs to be refreshed. 当我从先前的活动回来时,需要刷新MyDetailFragment中的数据。
i think ur recreating fragments multiple times, u do new MyListFragment everytime on onCreate function, call findFragmentByTag to get the existing fragment and set that, if null (first time) then create one
/ here is some code mate, if this doesnt work and ur app has single fragment better to just create xml and have only a fragment tag in it, and set that xml in setContentView function * / / / 这是一些代码伴侣,如果此方法不起作用,并且您的应用程序最好使用单个片段,则只需创建xml且其中仅包含fragment标签,然后在setContentView函数中设置该xml * /
// declare following member variable
MyFragment _fragment;
// in onCreate function, call this method
private void setupFragment()
{
_fragment = (MyFragment)getFragmentManager().findFragmentByTag("MyFragment");
if(null == _fragment)
{
_fragment = new MyFragment();
}
// now do the fragment transaction
FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.add(containerId, _fragment, "MyFragment"); // here tag is important
trans.commit();
}
Try this 尝试这个
MyDetailFragment fragment = new MyDetailFragment();
@Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(fragmentID, new MyListFragmentt())
.replace(detailFragmentID, fragment).commit();
}
}
@Override
protected void onRestart() {
if(fragment != null) {
getSupportFragmentManager().beginTransaction().replace(detailFragmentID, fragment).commitAllowingStateLoss();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.