简体   繁体   English

facebook如何保存碎片状态(活动被破坏时)?

[英]What does facebook do to save the fragment state(when activity is destroyed)?

I got from Are fragments saved by default with savedInstanceState? 我来自片段是否默认情况下使用saveInstanceState保存? that to "To keep a fragment when an Activity gets destroyed, so it automatically reataches, you should call `Fragment.setRetainInstance(true)'" “为了在Activity被销毁时保留片段,以便它自动重新绑定,您应该调用`Fragment.setRetainInstance(true)'”

However on https://developers.facebook.com/docs/android/login-with-facebook/v2.1#dialogs , they did not use this method in the constructor of the fragment but was still able to retain the fragment in oncreate Their code for doing so is 但是在https://developers.facebook.com/docs/android/login-with-facebook/v2.1#dialogs上 ,他们没有在片段的构造函数中使用此方法,但仍然能够将片段保留在oncreate中他们这样做的代码是

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

if (savedInstanceState == null) {
    // Add the fragment on initial activity setup
    mainFragment = new MainFragment();
    getSupportFragmentManager()
    .beginTransaction()
    .add(android.R.id.content, mainFragment)
    .commit();
} else {
    // Or set the fragment from restored state info
    mainFragment = (MainFragment) getSupportFragmentManager()
    .findFragmentById(android.R.id.content);
}

} }

Does anyone what trick they used to retain that fragment without using setretaininstance? 有人在不使用setretaininstance的情况下保留该片段的技巧是什么?

There is no trick, the FragmentManager is responsible for managing the fragments, it keeps a list of fragments added using fragment transactions and handles the back stack. 没有技巧, FragmentManager负责管理片段,它保留使用片段事务添加的片段列表,并处理反向堆栈。 When the orientation change happens, those fragments are going to be recreated automatically in order to recreate the view hierarchy as it was before the orientation change. 发生方向更改时,将自动重新创建这些片段,以重新创建视图层次结构,就像以前的方向更改一样。 Keep in mind that those recreated fragments are new instances. 请记住,这些重新创建的片段是新实例。 All you need to do is avoid creating the fragment if the bundle is not null, because is going to be recreated automatically. 您要做的就是避免在bundle不为null的情况下创建片段,因为它将自动重新创建。 And you can ask the FragmentManager for a reference to the fragment: 您可以要求FragmentManager引用该片段:

mainFragment = (MainFragment) getSupportFragmentManager()
    .findFragmentById(android.R.id.content);

Now, when you set setRetainInstance(true) , the fragment is not going to be destroyed when the orientation change happens (onDestroy() is not going to be called), and the exact same instance of the fragment is going to be attached to the new Activity after the orientation change (onCreate() is not going to be called, because the fragment was never destroyed). 现在,当您设置setRetainInstance(true)时,方向发生变化时片段将不会被破坏(onDestroy()将不会被调用),并且片段的完全相同的实例将被附加到方向更改后的新Activity(将不会调用onCreate(),因为该片段从未被销毁)。 This is helpful because the instance retains all its data accross the orientation change. 这是有帮助的,因为实例会在方向更改时保留其所有数据。

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

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