Retain the Fragment object while rotating

后端 未结 6 1244
滥情空心
滥情空心 2020-12-01 00:18

I have developed an app in Honeycomb and I am using fragments.
This is my app

  • I have an Activity (Say A1) and in that there is a fragment
  • Initi
6条回答
  •  时光取名叫无心
    2020-12-01 00:38

    By default Android will retain the fragment objects. In your code you are setting the homeFragment in your onCreate function. That is why it is allways some homeFragment or fl what ever that you set in onCreate.

    Because whenever you rotate, the onCreate will execute and set your fragment object to the first one

    So the easy solution for you is check whether savedInstanceState bundle is null or not and set the fragment object

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        if(null == savedInstanceState) {
            // set you initial fragment object 
        }
     }
    

提交回复
热议问题