0

I'm still working on passing data between each fragment in a tab navigation app. Initially I was using callbacks to set and get the value, but that didn't work properly. I switched to the current method of simply calling methods in the Activity to set and get values, but that's acting strange as well.

When I run the app the first fragment (first tab) gets the value, and is able to change it. When I swipe to the second tab, the value is not what it should be. When I swipe to the third tab, the value is correct. The value is persistent on the first and third tab (fragment), but the second tab doesn't want to cooperate.

Example:

  • Start - persistent_value = 0
  • tab1 displayed - value shown as 0
  • Change value on tab1 - persistent_value = 9
  • Swipe to tab2 - value shown as 0
  • Swipe to tab3 - value shown as 9
  • Change value on tab3 - persistent_value = 34
  • Swipe to tab2 - value shown as 0
  • Swipe to tab1 - value shown as 34

I'm not sure why the second tab is not updating, while both the first and third are. Am I doing something wrong, or is there something I'm not aware of here?

Here's some example code I'm using to try and solve the problem.

public class MainActivity extends Activity implements ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    int persistent_value = 0; 

    public void setPersistentValue(int value) {
        this. persistent_value = value;
    }

    public int getPersistentValue() {
        return this.persistent_value;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(), this);

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

         mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

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

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

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        private Context context;

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

        public SectionsPagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getResources().getString(R.string.tab_one_title);
                case 1:
                    return getResources().getString(R.string.tab_two_title);
                case 2:
                    return getResources().getString(R.string.tab_three_title);
            }
            return null;
        }
    }

    public class MainFragment extends Fragment {

        private static final String ARG_SECTION_TYPE = "section type";

        int persistent_value;

        public MainFragment(){}

        public MainFragment(int sectionNumber) {
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_TYPE, sectionNumber);
            setArguments(args);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            persistent_value = ((MainActivity)getActivity()).getPersistentValue;

            TextView textView = (TextView) rootView.findViewById(R.id.the_value);
            textView.setText(Integer.toString(this.persistent_value));

            return rootView;
        }

        @Override
        public void onPause() {
            super.onPause();
            ((MainActivity)getActivity()).setPersistentValue(this.persistent_value);
        }

        @Override
        public void onResume() {
            super.onResume();
            persistent_value = ((MainActivity)getActivity()).getPersistentValue;

            View rootView = getView();
            TextView textView = (TextView) rootView.findViewById(R.id.the_value);
            textView.setText(Integer.toString(this.persistent_value));
        }
    }
}
Community
  • 1
  • 1
Tester101
  • 8,042
  • 13
  • 55
  • 78

1 Answers1

0

It seems you are in a wrong way.

if you need to pass data between fragments, you need to use an interface and also you are not going to pass data directly from one fragment to another, instead you are going to pass it to activity first then to the receiver fragment

please refer this link for the fragment - fragment communication

http://developer.android.com/training/basics/fragments/communicating.html

Sam
  • 4,046
  • 8
  • 31
  • 47