Orientation change using fragments

I have application which uses fragments. Depending on screen size and orientation I'm displaying different layouts:
1. layout-fragment - list of choices
2. layout-fragment - display selected choice
I have problem with handling orientation change in large screen. In portrait mode I'm displaying both fragments in one layout but in landscape mode if user has chosen to display sth I'm only displaying 2nd fragment activity. When in this state orientation is changed back to portrait mode I would like to display both fragments again but instead I'm displaying again 2nd layout fragment activity.
I understand that it's android activity management but what is the best way to make it work as I would like to have (always display 1 and 2 fragment in portrait mode)?

Thanks for any suggestions!


This is because your second activity (showing only one frag) is re-created on orientation change.

What you can do here is to check orientation in your onCreate, if portrait - launch Activity_1 with suitable intent so that it can start second frag for correct details. Naturally you must also edit Activity_1 to check for this intent in onCreate..

Some example code where intent has action ACTION_VIEW and uri for detail object.

Activity_2 (one fragment - should only be used in landscape)

// in method onCreate
// assume orientation is checked and boolean set
if (isPortraitOrientation) {
   startActivity(suitableIntent); // ACTION_VIEW with uri
   finish();
   return;
} else {
   // load fragment
}

Activity_1 (showing two fragments if orientation allows

// check intent
// assume orientation checked and boolean set  
final Intent intent = getIntent();
final String action = intent.getAction();
Log.v(TAG, " - action: " + action);
if (isPortraitOrientation && Intent.ACTION_VIEW.equals(action)) {
   // load fragment with selected choice
}

Recommended orientation check: Check orientation on Android phone

链接地址: http://www.djcxy.com/p/88718.html

上一篇: 碎片Viewpager不同的屏幕尺寸

下一篇: 方向改变使用片段