So I was building a simple Quiz App which uses two fragments, one for multiple choice questions and one for free answer questions (type your own answer).

Now in order to display the questions correctly I need to use positions (1 through 5) to display the correct questions at the correct time.

I got the .replace working to replace my fragment but it is replacing the same fragments over and over (which is what i wanted). But now i can't get the fragment to update the questions correctly because it's not updating the position.

If i can get this working on one of the fragments I will also be able to get it working on the other fragment (I hope).

I need to be able to do this back and forth.

McQuestionFragment.java

```
public class McQuestionFragment extends Fragment {
    private int position;

    //Refresh method??? Im clueless how to implement this. Needs to update the position
    private void updatePosition(int position){
        this.position = position;
    }

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

        //TODO:: get the checkboxes and set possible answers based on question you are current at (using the position)
        CheckBox cb = (CheckBox)view.findViewById(R.id.checkBox);
        cb.setText(String.valueOf(this.position));

        return view;

    }
}

```

MainActivity.java

```
public class MainActivity extends AppCompatActivity {

    private FragmentManager fm = getSupportFragmentManager();
    private McQuestionFragment mcFragment;
    private QuestionFragment questionFragment;

    private int shownId;
    private int progress;

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

        questionFragment = new QuestionFragment();
        mcFragment = new McQuestionFragment();
        fm.beginTransaction().add(R.id.container, questionFragment).commit();

        //Go forward
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progress = progress + 1;
                if (shownId == 1) {
                    shownId = 2;
                    setProgressBar();
                    mcFragment.updatePosition(progress);
                    fm.beginTransaction().replace(R.id.container, mcFragment).commit();
                } else if (shownId == 2) {
                    shownId = 1;
                    setProgressBar();
                    fm.beginTransaction().replace(R.id.container, questionFragment).commit();
                }
            }
        });

        //Go back
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(shownId == 2){
                    shownId = 1;
                    progress = progress -1;
                    setProgressBar();
                    fm.beginTransaction().replace(R.id.container, questionFragment).commit();
                }else if(shownId == 1){
                    shownId = 2;
                    progress = progress -1;
                    setProgressBar();
                    mcFragment.updatePosition(progress);
                    fm.beginTransaction().replace(R.id.container, mcFragment).commit();
                }
            }
        });
    }   
}   

```

I have tried to use .attach and .detach but I couldn't get it to work myself. Any help would be appreciated.

~Thank you in advance~

EDIT 1::
Using Radu's answer i tried to do the following thing but it is giving me a nullpointerexception.

```
public class McQuestionFragment extends Fragment {
    private int position;
    private CheckBox cb;

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

        //TODO:: get the checkboxes and set possible answers based on question you are current at (using the position)
        this.cb = (CheckBox)view.findViewById(R.id.checkBox);
        cb.setText(String.valueOf(this.position));

        return view;

    }   

    //Refresh method??? 
    public void updatePosition(int position){
        this.position = position;

        //NULLPOINTER
        cb.setText(String.valueOf(this.position));
    }
}

```

EDIT 2::
Edited the MainActivity.java but it still gives me a nullpointer exception. Updated code:

```
if (shownId == 1) {
                shownId = 2;
                setProgressBar();
                fm.beginTransaction().replace(R.id.container, mcFragment).commit();
                mcFragment.updatePosition(progress);
            } else if (shownId == 2) {
                shownId = 1;
                setProgressBar();
                fm.beginTransaction().replace(R.id.container, questionFragment).commit();
            }

```

EDIT 3::
Still getting the nullpointer even with .attach and .detach

```
b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progress = progress + 1;
                if (shownId == 1) {
                    shownId = 2;
                    setProgressBar();
                    mcFragment.updatePosition(progress);
                    fm.beginTransaction().detach(questionFragment).attach(mcFragment).replace(R.id.container, mcFragment).commit();
                } else if (shownId == 2) {
                    shownId = 1;
                    setProgressBar();
                    fm.beginTransaction().replace(R.id.container, questionFragment).commit();
                }
            }
        });

```