Note that there are some explanatory texts on larger screens.

plurals
  1. POFragment design: Adapting to multiple screen layouts by showing/hiding fragments within a single Activity?
    primarykey
    data
    text
    <p>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:</p> <ol> <li>The <a href="http://developer.android.com/guide/topics/fundamentals/fragments.html" rel="noreferrer">Fragments</a> document on Android Developer guide.</li> <li><a href="http://code.google.com/p/iosched/source/checkout" rel="noreferrer">Google IO app</a></li> <li>Fragments sample from <a href="http://actionbarsherlock.com/samples.html" rel="noreferrer">ActionBar Sherlock</a>.</li> </ol> <p>All of these advocate a multiple <code>Activity</code> approach: </p> <ul> <li>On a large screen, display a single <code>Activity</code> with multiple <code>Fragment</code>s</li> <li>On a smaller screen, split up the <code>Fragment</code>s among multiple <code>Activity</code>s.</li> </ul> <p>I thought of another approach - a single <code>Activity</code> one:</p> <ul> <li>Have a single <code>Activity</code> with all the <code>Fragment</code>s in it.</li> <li>Depending on the screen size and orientation, show/hide the appropriate <code>Fragment</code>(s) (using <code>FragmentTransaction.show()</code> / <code>FragmentTransaction.hide()</code>) .</li> </ul> <p>To illustrate with the same "News article list/article contents" example that Android developer guide uses:</p> <ul> <li>Have the <code>News</code> activity containing both an <code>ArticleListFragment</code> and <code>ArticleReaderFragment</code>.</li> <li>On a tab, both fragments are always displayed.</li> <li>On a phone, the <code>ArticleReaderFragment</code> is initially hidden. When an article is selected from the list, the <code>ArticleListFragment</code> is hidden and the <code>ArticleReaderFragment</code> is shown.</li> </ul> <p>Has anybody used a similar approach? Are there any practical downsides this method might have? Does it seem better/worse compared to the multiple-activity way? For example, fragments cannot be shown/hidden in XML - one <strong>must</strong> use <code>FragmentTransaction</code> for this.</p> <hr> <h2>EDIT 1: Description of a Hypothetical Scenario</h2> <p>Imagine an app which can display up to three "panes" at a time on the screen. Further, these are the factors to consider:</p> <ul> <li>A phone can display only one pane at a time (regardless of portrait/landscape orientation)</li> <li>A 7-inch tablet can display 2 panes, split vertically in Portrait, and split horizontally in landscape mode.</li> <li>A 10+ inch tablet can display 2 panes, split vertically in Portrait; 3 panes split horizontally in landscape.</li> </ul> <p>For simplicity, lets keep TV screens out of the discussion.</p> <p>Now, translating this to design:</p> <ul> <li>We have three fragments: Frag1, Frag2 and Frag3.</li> <li>In the simplest case, All three fragments are in a single Activity (lets call it ActivityA). This is the 10-inch, landscape case.</li> <li>The other "simple" case is when each Fragment is in its own Activity - ActivityA contains Frag1; ActivityB contains Frag2 and ActivityC contains Frag3.</li> </ul> <p>So far, we have not considered anything which is significantly different from the News Reader example presented in the Android developer guide. The only major difference is having <em>three</em> fragments instead of two.</p> <p>Now, the case of 7-inch tabs which can accommodate only 2 fragments. How would this work? Note that there are two combinations possible here: </p> <ol> <li>Frag1 and Frag2 are being displayed.</li> <li>Frag2 and Frag3 are being displayed.</li> </ol> <p>I'm just unable to wrap my head around this. Do I do all of this within ActivityA? Do I just create an altogether new ActivityD? How many layouts would I need to create (I counted around 8)? Isn't it too many permuations?</p> <p>I do realize that the single-activity approach I proposed above might also not be a good fit for this scenario - since showing/hiding fragments in itself is non-trivial.</p> <p>Any suggestions on how to handle this without getting overwhelmed with layouts and combinations?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload