Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't wrap my head around Android Fragments
    primarykey
    data
    text
    <p>I'm struggling to wrap my head around Fragments and Activities.</p> <p>I'm writing an app which uses an ActionBar to navigate through "Featured Audio Items" List, and a List of "Programmes" which contain their own list of "Audio Items". I'm using ActionBarSherlock and the compatibility packages.</p> <p>When you click on an ActionBar tab, it should display that activity/fragment. When you click on something in a list, it should open a new fragment/activity which displays that programme/audio item. </p> <p>I have looked at various Demos/Samples and understand there are a few ways to do this.</p> <p>My questions:</p> <ul> <li>What's the structure of the programme? <ul> <li>e.g. An activity for every fragment? One activity with many fragments?</li> </ul></li> <li>How do you navigate the fragments? <ul> <li>e.g. Intents?</li> </ul></li> <li>How do I have a persistent ActionBar across all activities?</li> </ul> <p>Thanks for any help you can give</p> <h2>EDIT - Answering my own question:</h2> <p>So I've gone for this approach:</p> <ul> <li>Main Activity which initializes ActionBar, handles main Fragment Navigation.</li> <li>Each main Fragment has an onClickListener</li> <li>The onClickListener replaces the currently showing Fragment with a new one, initialized to show the right item</li> </ul> <p>This is based off the ActionbarSherlock FragmentLayoutSupport demo I mention in the comments.</p> <p><strong>Main List Fragment snippet:</strong></p> <pre><code>//public class ProgrammesFragment extends SherlockListFragment @Override public void onListItemClick(ListView l, View v, int position, long id) { showDetails(id); } /** * Helper function to show the details of a selected item, either by * displaying a fragment in-place in the current UI, or starting a * whole new activity in which it is displayed. */ void showDetails(long index) { // Check what fragment is currently shown, replace if needed. ProgrammeFragment details = (ProgrammeFragment) getFragmentManager().findFragmentByTag(ProgrammeFragment.TAG); if (details == null || details.getShownIndex() != index) { // Make new fragment to show this selection. details = ProgrammeFragment.newInstance(index); // Execute a transaction, replacing any existing fragment // with this one inside the frame. FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.remove(getFragmentManager().findFragmentByTag(ProgrammesFragment.TAG)); ft.add(android.R.id.content, details, ProgrammeFragment.TAG); ft.addToBackStack(null); ft.commit(); } } </code></pre> <p><strong>Details Fragment snippet:</strong></p> <pre><code>//public class ProgrammeFragment extends SherlockListFragment /** * Create a new instance of ProgrammeFragment, initialized to * show the text at 'index'. */ public static ProgrammeFragment newInstance(long index) { ProgrammeFragment f = new ProgrammeFragment(); // Supply index input as an argument. Bundle args = new Bundle(); args.putLong("programme_id", index); f.setArguments(args); return f; } public long getShownIndex() { return getArguments().getLong("programme_id", 0); } </code></pre> <p>The details fragment is using a LoaderManager on top of a SQL ContentResolver, and calls getShownIndex to build the conditions for the select query.</p> <p>This works (for now), not sure about the memory usage or any of that kind of thing. But we'll see!</p> <p>Seems SO is good for getting thoughts clearer in your mind so you can answer your own questions :P</p>
    singulars
    1. This table or related slice is empty.
    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.
    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