littleK
littleK

Reputation: 20123

Problems with Android FragmentActivity and Tabs

I have successfully converted my TabActivity into a FragmentActivity, with the tab content being Fragments. I am running two issues:

  1. Each of my tabs contain EditText's. When I change the value of an EditText on TAB1, switch to TAB2, and then switch back to TAB1, the value of the EditText has been reset.

  2. If start a new activity from one of my tab Fragments, and then go back to the existing FragmentActivity, then the tab content disappears. I was originally having this issue when using a TabHost, which is why I spent the time to convert everything to Fragments...

Regarding #1, I'm assuming the issue has to do with savedInstanceState. I followed Google's example exactly, using the following in onCreate() of my FragmentActivity:

initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
    mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}

Additionally, here is my onSaveInstanceState() method of my FragmentActivity:

protected void onSaveInstanceState(Bundle outState) {
    outState.putString("tab", mTabHost.getCurrentTabTag());
    super.onSaveInstanceState(outState);
}

Here is what one of my tab Fragment's looks like:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }

    return (LinearLayout) inflater.inflate(R.layout.priority_boxes_tab,
            container, false);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);

    ArrayList<Supply> list = new ArrayList<Supply>();

    // More code here

}

Should I be doing anything else? I have about 15 EditText fields on the screen. Do I need to somehow save all of those values and set it so that the fields will repopulate with those values the next time?

Regarding #2, I have no idea what else I can do. Does anyone have any suggestions?

Thanks!

Upvotes: 2

Views: 1483

Answers (1)

Jacob Phillips
Jacob Phillips

Reputation: 9264

I can only answer the first issue. Yes you have to save each edittext's contents manually if you want it to be restored. If they don't need to be retained across application runs, save them in the bundle in onSaveInstanceState and restore them in onCreate and/or onRestoreInstanceState. If you need the values to be persistent, use SharedPreferences or write them to another file or database. Preferences is probably easiest.

Upvotes: 2

Related Questions