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

后端 未结 6 1311
不知归路
不知归路 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:06

    When you are inside an activity and need to go to a fragment use below

    getFragmentManager().beginTransaction().replace(R.id.*TO_BE_REPLACED_LAYOUT_ID*, new tasks()).commit();

    But when you are inside a fragment and need to go to a fragment then just add a getActivity(). before, so it would become

    getActivity().getFragmentManager().beginTransaction().replace(R.id.*TO_BE_REPLACED_LAYOUT_ID*, new tasks()).commit();

    as simple as that.

    The *TO_BE_REPLACED_LAYOUT_ID* can be the entire page of activity or a part of it, just make sure to put an id to the layout to be replaced. It is general practice to put the replaceable layout in a FrameLayout .

提交回复
热议问题