Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 893
WenChao Avatar asked Sep 24 '15 00:09

WenChao


People also ask

When onResume method is called in android?

onResume() is one of the methods called throughout the activity lifecycle. onResume() is the counterpart to onPause() which is called anytime an activity is hidden from view, e.g. if you start a new activity that hides it. onResume() is called when the activity that was hidden comes back to view on the screen.

What is Runtime permission in android?

Runtime permissions prevent apps from gaining access to private data without a user's consent, and provide them with additional context and visibility into the types of permissions that applications are either seeking, or have been granted.

What is onStart method in android?

The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive. For example, this method is where the app initializes the code that maintains the UI.

How does activity come to the foreground?

Activity or dialog appears in foreground When the covered activity returns to the foreground and regains focus, it calls onResume() . If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state.


1 Answers

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.

like image 149
nikhil bansal Avatar answered Oct 19 '22 21:10

nikhil bansal