onCreate() and onCreateView() invokes a lot more than required (Fragments)

前端 未结 6 2030
不知归路
不知归路 2021-01-30 10:19

Can somebody explain why the onCreate() and onCreateView() are being invoked so many times which increments with each orientation change?

Here

6条回答
  •  情深已故
    2021-01-30 11:01

    I can't point to the documentation which explains this, but the solution is to only create and add the fragment when the activity first loads, like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        if (savedInstanceState == null) {
            Fragment fg = new Right();
            getFragmentManager().beginTransaction().add(R.id.right_frag, fg)
                .commit();
        }
        Log.i("Main", "onCreate()");
    }
    

提交回复
热议问题