如何使用片段处理方向更改?

时间:2016-06-22 03:22:55

标签: android android-fragments android-orientation

我现在有2个片段,一个片段处理纵向模式,然后是另一个处理横向模式。但问题是,从纵向旋转到横向然后再回到纵向。它不会显示在第一个肖像模式上显示的相同内容。有没有可以解决这个问题的代码?

此代码位于片段持有者中:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frag_holder);

    FragmentManager fm = getSupportFragmentManager();

    final Fragment fragment = Frag.newInstance(); //Portrait layout
    final Fragment fragment2 = Frag2.newInstance(); //Landscape layout

    int orientation = getResources().getConfiguration().orientation; //check whether is it portrait or landscape


    if(orientation == Configuration.ORIENTATION_PORTRAIT){
        Fragment fragTAG = fm.findFragmentByTag(TAG_P);
        if(fragTAG == null){
            Log.i("test","test");
                fm.beginTransaction()
                        .replace(R.id.fragPlaceHolder, fragment, TAG_P)
                        .commit(); //Portrait
        }
        else{
            fm.beginTransaction().replace(R.id.fragPlaceHolder,fragTAG).commit();
        }

    }
    if(orientation == Configuration.ORIENTATION_LANDSCAPE){
        Fragment fragTAG = fm.findFragmentByTag(TAG_L);
        if(fragTAG == null){
                fm.beginTransaction()
                        .replace(R.id.fragPlaceHolder, fragment2, TAG_L)
                        .commit(); //Landscape
        }
        else{
            fm.beginTransaction().replace(R.id.fragPlaceHolder,fragTAG).commit();
        }
    }

}

}

4 个答案:

答案 0 :(得分:4)

第1步: 在活动中添加配置更改

    <activity android:name=".ui.createtasks.CreateTaskActivity"
        android:configChanges="orientation|screenSize|keyboardHidden" > </activity>

第2步:    将您的修改文字值添加到onSaveInstanceState

 @Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
     outState.putCharSequence(KEY_TITLE, et_text.getText().toString());
 }

第3步:  通过onViewStateRestored

获取已保存的修改文字值
    @Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);

    String savedTitle = null;
    if (savedInstanceState != null) {
        savedTitle = savedInstanceState.getString(KEY_TITLE);
        et_text.setText(savedTitle);

    }

}

答案 1 :(得分:1)

您可以在两个片段中的onCreate()方法中调用setRetainInstance(True);

创建一个无头片段(没有UI的片段)来缓存数据。

第三个选项是使用onSaveInstanceState(Bundle outState)缓存数据并使用onCreateView()方法中的Bundle savedInstanceState重新显示数据。

答案 2 :(得分:0)

当有活动轮换时,活动关闭并重新打开onDestroy并调用onCreate。 如果你想保存数据并在另一个旋转中重新加载它你可以使用onSaveInstanceState方法:

protected void onSaveInstanceState(Bundle outState)

例如:

@Override
public void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    outState.putString("name", "David");
    outState.putInt("age", 17);
}

并在onCreate

中重新加载数据
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    …
    if(savedInstanceState != null)
    {
         savedInstanceState.getString("name");
         savedInstanceState.getInt("age");
    }
}

答案 3 :(得分:0)

在清单文件中添加代码

<activity
        android:name=".file_name"//add your activity name
        android:label="label" //add label
        android:configChanges="orientation|screenSize|keyboardHidden"
        android:theme="@style/AppTheme.NoActionBar" />