Question

I built a quiz and in my fragment the question + answers are shown. Every time the user chooses an answer, a dialog is shown and the user can decide whether or not to keep on playing. At the moment i react to it as following:

@Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getActivity(), ActivityQuiz.class));
                getActivity().overridePendingTransition(0, 0);
            }

but this seems slow. I'd rather reload the fragment, like when the screen rotates. How can I do this?

This is the rest of the code:

public static class PlaceholderFragment extends Fragment {

    TextView tv_question;
    Button btn_answer_1;
    Button btn_answer_2;
    Button btn_answer_3;
    JSONArray jArray = null;

    public PlaceholderFragment() {
    }

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

        ...
        btn_answer_1.setOnClickListener(btn_answer_listener);
        btn_answer_2.setOnClickListener(btn_answer_listener);
        btn_answer_3.setOnClickListener(btn_answer_listener);

        ...

        return rootView;
    }

 public OnClickListener btn_answer_listener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            int id = v.getId();

            if (id == R.id.btn_answer_1 && Globals.getCORRECT_ANSWER() == 1) {
                // correct
                answerCorrect();
            }
            else if (id == R.id.btn_answer_2 && Globals.getCORRECT_ANSWER() == 2) {
                // correct
                answerCorrect();
            }
            else if (id == R.id.btn_answer_3 && Globals.getCORRECT_ANSWER() == 3) {
                // correct
                answerCorrect();
            }
            else {
                answerWrong();
            }

        }

    };

    private void answerCorrect() {

        //Globals.incrementAnswersTotal(); // got to become sharedPreference
        Globals.INCREMENT_ANSWERS_ROUND();
        //Globals.incrementCorrectAnswersTotal(); // sharedPreference
        Globals.INCREMENT_CORRECT_ANSWERS_ROUND();

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setIcon(getResources().getDrawable(R.drawable.quiz_ic_dialog_correct));
        builder.setTitle(getResources().getString(R.string.ad_correct_answer_heading));
        builder.setMessage(getResources().getString(R.string.ad_correct_answer_text) + "\n\n" + Globals.getREMARK());
        builder.setPositiveButton(R.string.ad_keep_on, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getActivity(), ActivityQuiz.class));
                getActivity().overridePendingTransition(0, 0);
            }

        });
        builder.setNegativeButton(R.string.ad_to_menu, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getActivity(), ActivityAppLaunch.class));
            }

        });
        builder.setCancelable(false);

        AlertDialog alert = builder.create();
        alert.show();

    }

    private void answerWrong() {

        //Globals.incrementAnswersTotal(); // got to become sharedPreference
        Globals.INCREMENT_ANSWERS_ROUND(); 

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setIcon(getResources().getDrawable(R.drawable.quiz_ic_dialog_wrong));
        builder.setTitle(getResources().getString(R.string.ad_wrong_answer_heading));
        builder.setMessage(getResources().getString(R.string.ad_wrong_answer_text) + "\"" + Globals.getCORRECT_ANSWER_TEXT() + "\"" + "\n\n" + Globals.getREMARK());
        builder.setPositiveButton(R.string.ad_keep_on, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getActivity(), ActivityQuiz.class));
                getActivity().overridePendingTransition(0, 0);
            }

        });
        builder.setNegativeButton(R.string.ad_to_menu, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getActivity(), ActivityAppLaunch.class));
            }

        });
        builder.setCancelable(false);

        AlertDialog alert = builder.create();
        alert.show();

    }

}
Was it helpful?

Solution

Doing everything in onCreateView(...) is probably not ideal. Your best option is to move everything related to loading a question to its own method. In onCreateView(...), inflate your views and store local references to them. Then, override one of the fragment lifecycle methods that are garanteed to be called after that, such as onActivityCreated() and call the loading method from there; this would get the question and populate the views with the right data. Then, whenever you want a new question, simply call that loading method to repopulate the views with the new questions and answers. I can provide pseudocode if needed.

Edit: here is some pseudocode illustrating what I mean. This is by no means valid code but it is the basis of what you should be doing.

class Fragment {
    private TextView question
    private TextView answer

    onCreateView {
        View v = inflate view
        question = view.findView(question)
        answer = view.findView(answer)
    }

    onActivityCreated {
        loadQuestionAndAnswer()
    }

    loadQuestionAndAnswer {
        JSON json = Interwebs.getJSON()
        question.setText(json.getQuestion())
        answer.setText(json.getAnswer())
    }

    Dialog.onClick {
        loadQuestionAndAnswer()
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top