Fragment design: Adapting to multiple screen layouts by showing/hiding fragments within a single Activity?

前端 未结 4 2067
攒了一身酷
攒了一身酷 2020-12-13 00:08

I am trying to understand how to use Fragments to create apps that adapt well to multiple screens and layouts. I have studied a few examples:

  1. The Fragments doc
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 01:05

    Thank you for this question and your insights. I used multiple-activity approach until I stepped into a problem. My app is like the doc-example news reader. Consider this scenario:

    1. (initial state) I'm using a tablet in landscape mode (two-pane), activity A is displaying article list on the left and some article on the right.
    2. I rotate the device, now activity A is in single-pane mode and displays a list of articles.
    3. I click on a list item, activity B starts with aritcle opened.
    4. Now I rotate the device back to landscape mode.

    What's happening? Activity B is displaying an article full-sized, while of course I want to go back to two panes. I'm not sure how to solve this nicely. Of course activity B may check for multi-pane mode and finish itself, and activity A may check what was the last article displayed... That looks ugly. Thoughts?

    PS I have a suspicion that GMail app uses single-activity approach. I don't see any activity transition when I select an e-mail from the list. It also behaves properly in the scenario I described.

提交回复
热议问题