关于android:fragment如何恢复其视图状态? | 珊瑚贝

How fragment is restoring its view state?


我正在尝试探索 android 框架如何管理 Fragment,通过我的研究,我了解了很多我不了解 Fragment 的新事物,但我一度陷入困境并且无法理解看看这是怎么发生的。

请先尝试了解我的情况。它是这样的:
我有一个 Activity 一个一个地添加两个片段。首次加载活动时,使用以下代码将片段 A 附加到它:

1
2
3
4
5
6
7
8
9
private void initFirstFragment(){
    Bundle bundle = new Bundle();
    bundle.putString(“TEXT_TO_SHOW”,”FIRST ACTIVITY\
FIRST DUMMY FRAGMENT”);
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frameLayoutFragmentContainer, FirstDummyFragment.newInstance(bundle), FirstDummyFragment.class.getSimpleName());
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

Fragment A 加载时调用的这些回调方法

FirstDummyFragment: onCreate: savedInstanceState—>null

FirstDummyFragment: onCreateView: savedInstanceState—>null

FirstDummyFragment: onResume

现在在片段 A 中,我有一个编辑文本,我在其中输入了一些文本。

当在 Activity 中单击按钮时,使用以下代码将片段 B 添加到同一容器中:

1
2
3
4
5
6
7
8
9
public void openSecondFragment() {
    Bundle bundle = new Bundle();
    bundle.putString(“TEXT_TO_SHOW”,”FIRST ACTIVITY\
SECOND DUMMY FRAGMENT”);
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frameLayoutFragmentContainer, SecondDummyFragment.newInstance(bundle), SecondDummyFragment.class.getSimpleName());
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

添加Fragment B后调用以下回调方法

SecondDummyFragment: onCreate: savedInstanceState—>null

FirstDummyFragment: onDestroyView

SecondDummyFragment: onCreateView

SecondDummyFragment: onResume

当我按下返回按钮时,Fragment B 被销毁,Fragment A 进入前台并调用下面的回调方法

SecondDummyFragment: onDestroyView

SecondDummyFragment: onDestroy

SecondDummyFragment: onDetach

FirstDummyFragment: onCreateView: savedInstanceState—>null

FirstDummyFragment: onResume

片段 A 的编辑文本包含我之前在添加片段 B 之前输入的相同文本。我很困惑 android 是如何恢复片段 A 的视图状态,即使 savedInstanceState 为 null 并且 onCreateView 返回一个全新的视图再次创建片段 A 时的对象。

  • 这应该可以帮助你。 stackoverflow.com/questions/15313598/…
  • @akash93 这与我的问题无关。


我终于在这里找到了答案。


Android 就是这样设计的。在这种情况下,视图状态保存/恢复在 Fragment 内部调用。因此,每个在内部实现了视图状态保存/恢复的单个视图,例如带有 android:freezeText=”true” 的 EditText 或 TextView,将自动保存和恢复状态。使其显示与以前完全相同。

  • 我认为这是事实,但许多 Android 开发人员并不十分了解。围绕实例状态的方法似乎只引用片段的内部状态(即片段的实例变量),它应该保存在 savedInstanceState 包中并从包中恢复。但这仅在父活动保存其状态时发生(即在配置更改时,例如将活动返回堆栈或设备旋转)。当片段被回栈时,实例(包括它的状态)只保留需要恢复的视图。


来源:https://www.codenong.com/41536274/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9904.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?