Zheng Xian
Zheng Xian

Reputation: 320

How to handle orientation change using fragment?

I now have 2 fragment, one fragment handle portrait mode then another handle landscape mode. But the problem is that when rotate from portrait to landscape then back to portrait. It will not show the same thing that show on the first portrait mode. Is there any code that can solve this problem?

This code is inside the fragment holder:

@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();
        }
    }

}

}

Upvotes: 7

Views: 18890

Answers (4)

Arooter
Arooter

Reputation: 53

Add code in manifest file

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

Upvotes: 0

Sanaullah
Sanaullah

Reputation: 81

Step 1: Add Config changes in your activity

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

Step 2: Add your edit text values to onSaveInstanceState

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

Step 3: Get your Saved edit text values through 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);

    }

}

Upvotes: 6

Yanay Hollander
Yanay Hollander

Reputation: 427

When there is activity rotation, the activity closed and reopen the onDestroy and onCreate are called. if you want to save data and reload it in the other rotation you can do it with onSaveInstanceState method:

protected void onSaveInstanceState(Bundle outState)

for example:

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

and reload the data in the onCreate

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    …
    if(savedInstanceState != null)
    {
         savedInstanceState.getString("name");
         savedInstanceState.getInt("age");
    }
}

Upvotes: 1

Eric Liu
Eric Liu

Reputation: 1536

You can either call setRetainInstance(True); in the onCreate() methods in both Fragments.

Or

to create a headless-Fragment(a Fragment with no UI) to cache data.

A third option will be to use onSaveInstanceState(Bundle outState) to cache data and re-display the data by using Bundle savedInstanceState in the onCreateView() method.

Upvotes: 1

Related Questions