How to move from one fragment to another fragment on click of an ImageView in Android?

后端 未结 6 1299
不知归路
不知归路 2020-12-03 10:16

I have an ImageView. I want to move from one fragment to another fragment on a click of an Imageview, the same way like we can move from one activity to another using

<
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 11:08

    you can move to another fragment by using the FragmentManager transactions. Fragment can not be called like activities,. Fragments exists on the existance of activities.

    You can call another fragment by writing the code below:

            FragmentTransaction t = this.getFragmentManager().beginTransaction();
            Fragment mFrag = new MyFragment();
            t.replace(R.id.content_frame, mFrag);
            t.commit();
    

    here "R.id.content_frame" is the id of the layout on which you want to replace the fragment.

    you can also add the other fragment incase of replace.

提交回复
热议问题