Question

I'm using 3 tabs right now, and I can swap between them well and good. However, lets say I make some changes in the first tab (enter something into a textview), then go to the third tab and then come back, the changes disappear. This seems to be quiet obvious, since in my code I am setting the tab layout of the TabFragment based on my xml layout files. Is there a way to save the Tab's layout when we swipe on to a different tab, so that I can switch back to this saved tab layout later on?

(A funny thing I have observed is that the changes stay persistent when I switch between the first 2 tabs, but whenever I go to the third one and come back to the first, they disappear)

My main activity file (I got the code from this answer)

public class Tob extends FragmentActivity implements ActionBar.TabListener 
{

CollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCollectionPagerAdapter = new CollectionPagerAdapter(getSupportFragmentManager());

    final ActionBar actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() 
    {
        @Override

        public void onPageSelected(int position) 
        {
            actionBar.setSelectedNavigationItem(position);
        }

    });
    for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) 
    {
        actionBar.addTab(actionBar.newTab()
        .setText(mCollectionPagerAdapter.getPageTitle(i))
        .setTabListener(this));
    }

}

public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
{
}

public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
{
    mViewPager.setCurrentItem(tab.getPosition());   
}

public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
{

}

public class CollectionPagerAdapter extends FragmentPagerAdapter 
{
    final int NUM_ITEMS = 3; // number of tabs

    public CollectionPagerAdapter(FragmentManager fm) 
    {
        super(fm);      
    }

    @Override

    public Fragment getItem(int i) 
    {
        Fragment fragment = new TabFragment();
        Bundle args = new Bundle();
        args.putInt(TabFragment.ARG_OBJECT, i);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() 
    {       
        return NUM_ITEMS;       
    }

    @Override
    public CharSequence getPageTitle(int position) 
    {
        String tabLabel = null;
        switch (position) {
            case 0:
                tabLabel = "Tab1";
                break;
            case 1:
                tabLabel = "Tab2";
                break;
            case 2:
                tabLabel = "Tab3";
                break;

        }

        return tabLabel;
    }
}

public static class TabFragment extends Fragment 
{
    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {

        Bundle args = getArguments();
        int position = args.getInt(ARG_OBJECT);
        int tabLayout = 0;
        switch (position) 
        {
            case 0:
                tabLayout = R.layout.tob1;
                break;
            case 1:
                tabLayout = R.layout.tob2;
                break;
            case 2:
                tabLayout = R.layout.tob3;
                break;
        }

        View rootView = inflater.inflate(tabLayout, container, false);
        return rootView;

    }

}

My activity_main.xml file:

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Tob"/>

tob1.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/apppattern" 
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >

and so on. My hunch is that I will need to save the layout of a tab when it is switched, I'm just not sure where to do that.

Was it helpful?

Solution

Cross-posting: saving data on tabs

You can use Shared Preferences to store and retrieve user-inputs/strings/numbers etc from the tab.

First, declare a SharedPreference name tag in your class.

public static final String PREFS_NAME = "Blah";

Next, in your onTabUnselected method, save all form-related text data into the SharedPreference.

public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
{
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
     SharedPreferences.Editor editor = settings.edit();
     editor.putString("userText", (TextView)getViewById(R.id.myTextView).getText());
     editor.commit();
}

Finally, retrieve the SharedPreference text in onTabSelected method.

public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
{
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
     String myText = settings.getString("userText", "Default");
     ((TextView)getViewById(R.id.myTextView)).setText(myText);
}

Here is the original Android doc page http://developer.android.com/guide/topics/data/data-storage.html You can save primitives like Strings, floats, booleans etc using SharedPreferences. You can also save url strings for other resources like layouts/images and then retrieve them and dynamically create your views in tab1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top