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

点点圈 提交于 2019-12-06 20:46:12

问题


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, works fine. Backgrounds the app by pressing Home button, then turn off the Storage permission and open the app from recent task, app crashes.

The problem is instead of calling onResume() of the host Activity, onCreate() is called on the host Activity as well as onCreateView() of the Map fragment. Thus it is throwing Exceptions.

It seems like the app process is killed when permission is changed and thus Activity is recreated.

    09-24 14:42:55.071: E/AndroidRuntime(12918):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-24 14:42:55.071: E/AndroidRuntime(12918): Caused by: 
java.lang.NullPointerException: Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim' on a null object reference
09-24 14:42:55.071: E/AndroidRuntime(12918):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:718)
09-24 14:42:55.071: E/AndroidRuntime(12918):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
09-24 14:42:55.071: E/AndroidRuntime(12918):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)

From super.onStart() from the Activity


回答1:


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.




回答2:


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.




回答3:


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
        }
    }


来源:https://stackoverflow.com/questions/32751667/oncreate-gets-called-when-reopen-from-recent-task-after-permission-settings-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!