Note that there are some explanatory texts on larger screens.

plurals
  1. POActionBarSherlock with navlist: how to save instance of Fragments
    text
    copied!<p>I have a small App with one Activity and a lot of Fragments; I work with SherlockFragment. My Problem is that I dont know how to save the instance of a fragment to get the last Fragment that appears on a special navlist item.</p> <p>E.g.: I choose a navlist item e.g. "info". Then I get FragmentA, FragmentA extends from a SherlockListFragment, so I can choose a ListItem and then I get FragmentB. FragmentB extends also from a SherlockListFragment and so I get FragmentC and so on.</p> <p>Now the Problem is the following: When I choose another navlist item e.g. "home" I get FragmentD. Now I choose again my navlist item "info" and I want that FragmentC appears.</p> <p>In my Code-snippet I just replace the Fragments, but I need to know which fragment was shown as last.</p> <p>My SherlockFragmentActivity:</p> <pre><code>public class MainActivity extends SherlockFragmentActivity implements ActionBar.OnNavigationListener{ private FragmentHome mFragmentHome = null; private FragmentInfo mFragmentInfo = null; private static final String mListItemHome = "starthome"; private static final String mListItemInfo = "startinfo"; public void onCreate(Bundle savedInstanceState) { setTheme(R.style.My_Theme); super.onCreate(savedInstanceState); setContentView(R.layout.main_fragment); getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setHomeButtonEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(false); Context context = getSupportActionBar().getThemedContext(); // Navlist ArrayAdapter&lt;CharSequence&gt; list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_dropdown_item); list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item); getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); getSupportActionBar().setListNavigationCallbacks(list, this); mFm = getSupportFragmentManager(); //Init of my Fragments: mFragmentHome = new FragmentHome(); mFragmentInfo = new FragmentInfo(); } @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { FragmentTransaction ft = mFm.beginTransaction(); switch(itemPosition){ case 0: ft.replace(R.id.fragment_content, mFragmentHome); ft.addtoBackStack(mListHome); ft.commit(); return true; case 1: ft.replace(R.id.fragment_content, mFragmentInfo); ft.addtoBackStack(mListInfo); ft.commit(); return true; default: return false; } } } </code></pre> <p>My Fragments seem like this:</p> <pre><code> public class FragmentInfo extends SherlockListFragment{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //disable the HomeButton as "Up"-Button ActionBar actionBar = getSherlockActivity().getSupportActionBar(); actionBar.setHomeButtonEnabled(false); actionBar.setDisplayHomeAsUpEnabled(false); ... } @Override public void onListItemClick(ListView l, View v, int position, long id) { Bundle bundle = new Bundle(); bundle.putString("x", x ); bundle.putString("y", y); FragmentManager fm = getFragmentManager(); FragmentInfo fragment = new FragmentInfo(fm); fragment.setArguments(bundle); FragmentTransaction ft = fm.beginTransaction(); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.replace(R.id.fragment_content, fragment); ft.addToBackStack("info"); ft.commit(); } } </code></pre> <p>I really looked the whole day for a solution, but I didn't find soemthing that could help me. I think I need something like this for tabs: <a href="https://stackoverflow.com/questions/10082163/actionbarsherlock-tabs-multi-fragments">Actionbarsherlock + tabs + multi fragments?</a> Or something like a TabHost for Lists...</p> <p>I hope you can help me.</p> <p>With best regards</p> <p><strong>EDIT:</strong></p> <p>I just get the Problem with hide and show. In my xml-layout i've got a Framelayout with the id: fragment_content. This id was always used to replace one fragment by an other. Now I add another Framelayout with the id: fragment_info. I've done this to get the last Fragment that I replaced there with the FragmentManager.</p> <p>Now, my on NavigationItemSelected Method seems like this:</p> <pre><code>@Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { FragmentTransaction ft = mFm.beginTransaction(); switch(itemPosition){ case 0: if(mFragmentHome==null){ mFragmentHome = new FragmentHome(); ft.replace(R.id.fragment_content, mFragmentHome) ft.addToBackStack(mListItemHome); ft.commit(); }else{ if(mFragmentHome.isHidden()){ ft.show(mFragmentHome); ft.replace(R.id.fragment_content, mFragmentHome) ft.addToBackStack(mListItemHome); ft.commit(); } ft.hide(mFragmentInfo); } case 1: if(mFragmentInfo==null){ mFragmentInfo = new FragmentInfo(); ft.replace(R.id.fragment_info, mFragmentInfo) ft.addToBackStack(mListItemInfo); ft.commit(); ft.hide(mFragmentHome) }else{ ft.hide(mfragmentHome(); //here I get the last Fragment I choose under the navlist-Item "Info" Fragment f = getFragmentManager.findFragmentById(R.id.fragment_info); ft.replace(R.id.fragment_info, f); ft.addToBackStack(mListItemInfo); ft.commit(); } ft.hide(mFragmentHome); default: return false; } } } </code></pre> <p>My xml:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;FrameLayout android:id="@+id/fragment_content" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; &lt;FrameLayout android:id="@+id/fragment_info" android:layout_width="match_parent" android:layout_height="match_parent"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>But now I have another Problem with my Back-Button. When I choose Menu Info and browse through FragmentA to FragmentB to FragmentC, then I choose navlist-Item "Home" and again navlist-Item "Info", so I get FragmentC. But when I now press the BackButton the Activity will destroyed.</p> <p>But I want that after pressing the BackButton comes FragmentB -> FragmentA and now i can finish the Activity.</p> <p>I know I can override the Backbutton but I guess I make some mistakes. Perhaps I should work with detach and attach? I think there should be a simpler way, isnt't there?</p>
 

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