Android replace the current fragment with another fragment

前端 未结 6 1312
迷失自我
迷失自我 2020-11-28 09:23

I just started with fragment design for HoneyComb. I created two fragments. When i click a button in the left side fragment, a new fragment is created in right side. Meanwh

6条回答
  •  再見小時候
    2020-11-28 09:37

    Then provided your button is showing and the click event is being fired you can call the following in your click event:

    final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
    ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag"); 
    ft.commit(); 
    

    and if you want to go back to the DetailsFragment on clicking back ensure you add the above transaction to the back stack, i.e.

    ft.addToBackStack(null);
    

    Or am I missing something? Alternatively some people suggest that your activity gets the click event for the button and it has responsibility for replacing the fragments in your details pane.

提交回复
热议问题