开发者

Orientation change using fragments - wrong activity start

开发者 https://www.devze.com 2023-03-21 10:29 出处:网络
I have application which uses fragments. Depending on screen size and orientation I\'m displaying different layouts:1. layout-fragment - list of choices2. layout-fragment - display selected choice I h

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

0

精彩评论

暂无评论...
验证码 换一张
取 消