Android Fragments recreated on orientation change

后端 未结 4 2088
有刺的猬
有刺的猬 2020-12-17 15:42

I\'m developing an app that basically has an ActionBar. When my app starts, the Activity creates the fragments and attaches them to each tab, so when I switch I get differen

4条回答
  •  醉酒成梦
    2020-12-17 16:03

    You need to check for a savedInstanceState [edit: in your parent activity], and if it exists, don't create your fragments.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        if (savedInstanceState == null) {
             // Do your oncreate stuff because there is no bundle   
        }
    // Do stuff that needs to be done even if there is a saved instance, or do nothing
    }
    

提交回复
热议问题