onCreate() gets called when reopen from recent task after permission settings is changed

前端 未结 3 2035
离开以前
离开以前 2021-02-20 07:44

The question title may sounds complicated but here is my situation.

I have a map fragment within an activity. Simple. turn on Storage permission to allow display of Map,

相关标签:
3条回答
  • 2021-02-20 07:46

    Probably a bit late, still might help out others in the future :)

    You should check to see if savedInstanceState is null which is passed to onCreate(Bundle savedInstanceState). If it's null only then, launch the Fragment like so:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_layout_file);
    
            if (savedInstanceState == null) {
                // do fragment transactions here
            }
        }
    
    0 讨论(0)
  • 2021-02-20 07:51

    The workable solution now is just to call getSupportFragmentManager().popBackStackImmediate(); in onCreate() of the Activity to prevent from trying to recreate the map fragment.

    popBackStack() won't work in this case as it is asynchronous.

    0 讨论(0)
  • 2021-02-20 07:55

    I was having similar issues with the dynamic change in app's permission. After some dig what I understood might be helpful for you also.

    Changing permission cause termination of your application by the system and now if you open your app from overview screen, it will restart your application, which is the main reason for your crash because you might be performing an operation on fragment object, which is null now.

    An important point here to note is that before termination fragment state is saved in savedinstance object.

    To stop crash you should get fragment instance using this line of code -

    mapFragment = (MapFragment)getSupportFragmentManager().findFragmentByTag("MAP FRAG");
    

    Here "MAP FRAG" is a tag you have to give to your fragment.

    share if you have any confusion.

    0 讨论(0)
提交回复
热议问题