0

Is is possible to LOCKED fragments on button click? For example.. Im making a quiz app but it is in Fragment form.. after clicking the SUBMIT BUTTON. you must swipe to the next page or the QUESTION 2.java. now the thing is.. I want to disable the PREVIOUS fragment so you cannot go back to QUESTION 1. Thank you guys! my code..

QUESTION 1.JAVA

public class Question1 extends Fragment{

RadioButton q1a2;
Button btn1;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.question1, null);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    q1a2 = (RadioButton)getView().findViewById(R.id.q1a2);
    btn1 = (Button)getView().findViewById(R.id.btnq1);

    final SharedPreferences app_preferences =  PreferenceManager.getDefaultSharedPreferences(getActivity());
    btn1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            SharedPreferences.Editor editor = app_preferences.edit();
            Toast.makeText(getActivity(), "Submitted",  Toast.LENGTH_SHORT).show();

            if (q1a2.isChecked()){
                editor.putInt("answer_value", 1);
            } else {
                editor.putInt("answer_value", 0);
            }
            editor.commit();
        }
    });

}

}

QUESTION 2.JAVA

public class Question2 extends Fragment{

RadioButton q2a1;
Button btn2;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.question2, null);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    q2a1 = (RadioButton)getView().findViewById(R.id.q2a1);
    btn2 = (Button)getView().findViewById(R.id.q2_button);

    final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    btn2.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            SharedPreferences.Editor editor = app_preferences.edit();

            if (q2a1.isChecked()){
                editor.putInt("answer_value2", 1);
            } else {
                editor.putInt("answer_value2", 0);
            }
            editor.commit();
        }
    });
}
}

1 Answers1

0

You can remove the old fragments from the viewpager dynamically, as the user moves forward:

dynamically add and remove view to viewpager

Community
  • 1
  • 1
Eduard
  • 3,482
  • 2
  • 27
  • 45