Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different fragment wasn`t loaded when orientation changed (android)

I created a test project that has two different fragments to be shown in the same activity. One fragment is for landscape and the other for portrait.

# My unique activity
public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

# First Fragment
public class LandscapeFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false);
        v.setText("LANDSCAPE");
        return v;
    }
}

# Other Fragment
public class PortraitFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false);
        v.setText("PORTRAIT");
        return v;
    }
}

And I have two main.xml, one in layout/ and the other in layout-land/. Each main.xml points to the correct Fragment to be used.

<!-- layout-land/main.xml  -->
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment"
    android:name="br.luckcheese.test.LandscapeFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp" />

<!-- layout/main.xml  -->
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment"
    android:name="br.luckcheese.test.PortraitFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp" />

<!-- layout/fragment.xml  -->
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp" />

When I open the app on landscape the LandscapeFragment is shown, and when I open in Portrait the PortraitFragment is shown. So far, so good.

But if I open in landscape and rotate the device to portrait then the LandscapeFragment is reloaded and shown. Which is not the predicted behavior. The PortraitFragment should have been loaded.

And the same thing happens the other way around, if the device starts at the portrait orientation and then I rotate it to landscape: the PortraitFragment just gets reloaded, instead of having the LandscapeFragment loaded.

What am I doing wrong?

like image 578
Luckcheese Avatar asked Apr 05 '12 16:04

Luckcheese


People also ask

What happens when screen orientation changes in Android?

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and when the user enables multi-window mode). When such a change occurs, Android restarts the running Activity ( onDestroy() is called, followed by onCreate() ).

How do you change orientation when retaining data?

Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.

Can a fragment without a layout can be attached to an activity?

A fragment is not required to be a part of the Activity layout ; you may also use a fragment without its own UI as an invisible worker for the Activity but it needs to be attached to an Activity in order to appear on the screen.


1 Answers

Make sure that you don't have: android:config="orientation" in your manifest.xml

like image 162
Nhat Nam NGUYEN Avatar answered Sep 18 '22 15:09

Nhat Nam NGUYEN