0

I had create an FragmentPagerAdapter to display many question with the same Fragment.

This is the fragment :

QuestionFragment

When answer is give and validate, I disable button and EditText. But when I slide of two Fragment and come back to this, EditText and Button are no longer disable and the user can change value of the edit text.

This is the FragmentPagerAdapter :

    /**
     * A simple {@link Fragment} subclass.
     */
    public class PlayFragment extends FragmentPagerAdapter {

        //--Jouer un circuit
        private static Game currentGame = null; //La partie courante
        public static Circuit circ;
        private static List<QuestionReponse> questionsReponses = new ArrayList<>();

        private int PAGE_COUNT;
        private static ArrayList<String> listTitles = new ArrayList<>();

        public PlayFragment(FragmentManager fm) {
            super(fm);
            this.listTitles = new ArrayList<>();
            for (int i = 0; i < currentGame.getDifficulties().size(); i++) {
                this.listTitles.add(currentGame.getDifficulties().get(i).getName()); //Les difficultées choisis
            }
            this.PAGE_COUNT = currentGame.getDifficulties().size();
        }


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

        @Override
        public Fragment getItem(int position) {
            return QuestionFragment.newInstance(position + 1);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // Generate title based on item position
            return listTitles.get(position);
        }

        public static Game getCurrentGame() {
            return currentGame;
        }

        public static void setCurrentGame(Game currentGame) {
            PlayFragment.currentGame = currentGame;
        }


        public static ArrayList<String> getListTitles() {
            return listTitles;
        }

        public static void setListTitles(ArrayList<String> listTitles) {
            PlayFragment.listTitles = listTitles;
        }
    }

This is the StartGameFragment :

/**
 * A simple {@link Fragment} subclass.
 */
public class StartGameFragment extends Fragment implements AsyncResponse {

    PlayFragment playFragment;

    public StartGameFragment() {
        // Required empty public constructor
    }


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

        ((MainActivity) getActivity()).changeActionBarTitle(getString(R.string.nav_circuit_dl));
        MenuItem item = ((MainActivity) getActivity()).navigationView.getMenu().findItem(R.id.nav_dl_circuit);
        if (item != null) item.setChecked(true);

        //Allow to modify ActionBar menu in this fragment
        setHasOptionsMenu(true);
        //new JSONGetter().execute("");
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_start_game, container, false);

        ViewPager viewPager = (ViewPager) v.findViewById(R.id.viewpager);
        this.playFragment = new PlayFragment(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(playFragment);

        // Give the TabLayout the ViewPager
        TabLayout tabLayout = (TabLayout) v.findViewById(R.id.sliding_tabs);
        tabLayout.setupWithViewPager(viewPager);
        return v;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
    }


    @Override
    public void onDestroyView() {
        MenuItem item = ((MainActivity) getActivity()).navigationView.getMenu().findItem(R.id.nav_dl_circuit);
        if (item != null) item.setChecked(false);
        super.onDestroyView();
    }

    @Override
    public void onTaskDBCompleted(String result, Object entity, String method) {

    }

    @Override
    public void onTaskCompleted(String result, String method) {

    }
}

And finally QuestionFragment (I don't show all because too much code in this Fragment) :

/**
 * A simple {@link Fragment} subclass.
 */
public class QuestionFragment extends Fragment implements AsyncResponse {

    public QuestionFragment() {
        // Required empty public constructor
    }


    public static QuestionFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        QuestionFragment fragment = new QuestionFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
    }
}

I tried with this solution but that doesn't work for me !

Community
  • 1
  • 1
Eraseth
  • 503
  • 1
  • 6
  • 19

3 Answers3

1

There is another adapter available in v4 support library called FragmentStatePagerAdapter.

It saves internal state of fragments by default. You can override its saveState() and restoreState() methods to save/restore custom state data as well.

S.D.
  • 29,290
  • 3
  • 79
  • 130
-1

//Store your states here inside your fragment

 @Override
 protected void onSaveInstanceState(Bundle savedInstanceState) {
 super.onSaveInstanceState(outState);

 String stateToSave = edittextEditState.getText().toString();
 savedInstanceState.putString("saved_state", stateToSave);
}

}

//retrieve the data

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( savedInstanceState != null ) {
 Toast.makeText(this, savedInstanceState .getString("saved_state"),Toast.LENGTH_LONG).show();
 } 
 }
Community
  • 1
  • 1
Mrugesh
  • 4,381
  • 8
  • 42
  • 84
-1

Save the state of the fragment using savedInstanceState

Refer - http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Pooja Nair
  • 113
  • 7