O
O
orbit0702019-07-10 22:22:16
Android
orbit070, 2019-07-10 22:22:16

How to find the desired fragment after changing the screen orientation?

Hello.
I have one activity and let's say 100 fragments.
After rotating the screen, how to display that fragment, the same fragment that was displayed before the device was rotated?
I do something like this in Activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.my_layout);

        if (fragment instanceof MyFragment1) {
            goToMyFragment1();
        } else if (fragment instanceof MyFragment2) {
             goToMyFragment2();
        } else if (fragment instanceof MyFragment3) {
             ...
        }
    }

This is probably the wrong approach, especially with a large number of fragments. How to do it right? Thanks

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question