简体   繁体   English

如果活动布局更改,则在配置更改后恢复片段状态

[英]Restoring Fragment state after configuration change if Activity layout changes

My Activity has a one-pane layout in portrait and a two-pane layout in landscape, using Fragments. “我的活动”使用“片段”在纵向中具有一个窗格的布局,在横向中具有两个窗格的布局。 I'd like the system to restore all the views after an orientation change, but I don't know where to start. 我希望系统在更改方向后恢复所有视图,但是我不知道从哪里开始。

Is this even possible considering the layout is different (but similar) in the two orientations? 考虑到两个方向上的布局不同(但相似),这是否有可能?

If anyone has implemented this (or at least tried), please share you experience. 如果有人实施(或至少尝试过),请与您分享经验。

I urge you to use a Fragment with setRetainInstance(true) in its onCreate method. 我敦促您在其onCreate方法中使用带有setRetainInstance(true)的Fragment。 This makes the fragment retain its member variables across Activity configuration changes. 这使得片段在Activity配置更改中保留其成员变量。 Please read this blog post which explains the information you need: http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html 请阅读此博客文章,其中解释了您所需的信息: http : //www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

@Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

Which you can use later like this: 您可以稍后使用它:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }
}

Maybe I know what the problem is. 也许我知道问题出在哪里。 Hope my answer helps someone want to solve such problem. 希望我的回答可以帮助想要解决此类问题的人。 In case there are a Fragment in your Activity, you config that Activity in AndroidManifest.xml with 如果您的Activity中有一个Fragment,请在AndroidManifest.xml中使用以下命令配置该Activity

android:configChanges="keyboardHidden|orientation|screenSize"

then, you want to change the layout of the Activity when onConfigurationChanged triggered. 然后,您想在触发onConfigurationChanged时更改Activity的布局。 Once the Activity inflate a new layout or using setContentView(R.layout.new_layout); 一旦Activity膨胀了新的布局或使用setContentView(R.layout.new_layout); , the fragment will disappear, however, that fragment still running and just disappear from the view. ,该片段将消失,但是该片段仍在运行,并且仅从视图中消失。

So the problem is that fragment is attached to the previous layout, what you need to do: 因此,问题在于片段需要附加到先前的布局,您需要执行以下操作:

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
if (fm.findFragmentById(R.id.pane_list) == null) {
    // instantiate fragment and add to view
    mFragment = new ItemFragment();
    transaction.add(R.id.pane_list, mFragment );
} else {
    // fragment already exists, we re-attahced it
    mFragment = fm.findFragmentById(R.id.pane_list);
    transaction.detach(mFragment);
    transaction.attach(mFragment);
}
transaction.commit();

After detached from old layout and attached to new layout, problem solved. 从旧布局分离并附加到新布局后,问题解决了。 Correct me if there any wrong :) 如果有任何错误请指正:)

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

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