How do fragments of an activity get restored when the activity restarts?

后端 未结 1 394
故里飘歌
故里飘歌 2021-01-22 17:47

I am testing situation where a user goes into my app after the system has terminated the app process due to low RAM. I am seeing unexpected behavior and hoping to get some help

相关标签:
1条回答
  • 2021-01-22 18:23

    When you call super.onCreate(), Fragments automatically restore their current state when savedInstanceState is not null.

    Therefore if you're expecting to do one time set up by adding your intial Fragment, you should always surround it with an if (savedInstanceState == null) check:

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // I assume you accidentally left out these lines
        super.onCreate(savedInstanceState);
        setContentView(R.id.your_content_view);
    
        if (savedInstanceState == null) {
            FragmentA fragA = new FragmentA();
            FragmentTransaction fragmentTransaction = 
            getSupportFragmentManager().beginTransaction();
            fragmentTransation.replace(R.id.basic_frame, fragA);
            fragmentTransaction.commit();
        }
    }
    
    0 讨论(0)
提交回复
热议问题