3

when current activity jump to system setting page to disable permissions and then switch current activity again,the app crashed

Step 1: Opened app and gave all the necessary permissions

Step 2: Clicked Home button(So the app is in background)

Step 3: Manually changed the permissions in the Settings

Step 4: Launched the app from multitasks, now it crashes because of app context becomes invalid

Observed that app gets created again, don't understand why this happens. Any suggestions to rectify this issue would be welcome!

Cœur
  • 37,241
  • 25
  • 195
  • 267
uma
  • 259
  • 3
  • 22

3 Answers3

2

The most effective solution I found to this problem for my use case was to simply check a particular View that I identified as being null when returning to the app following changing a permission in my Settings screen. This resulted in several Views being null on resuming the app, causing a fatal error.

Basically, at the stage of changing a permission whilst your app is in the background, Android creates a new process for your app on resuming the app; invalidating your previous process. You can use a Log statement to record the differences using something like this for testing, which you should see that the Activity identifier is different:

Log.v("YOURTAGHERE", String.valueOf(activity));

I experienced the same problem and implemented the following to simply restart the app, which solved the problem for my use case:

if (mTabView == null) {
    Intent intent = new Intent(sActivity, SplashActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
    Intent.FLAG_ACTIVITY_CLEAR_TASK);
    sActivity.startActivity(intent);
    sActivity.finish();
}
Richard Ansell
  • 916
  • 1
  • 12
  • 28
  • I implemented the solution. I am checking if permission is granted if not then take action. Working fine till Android 11 but getting crash on Android 12. Is there any special handling for Android 12? – Shyamaly Lakhadive Aug 17 '22 at 02:45
1

In my case an app required location permission (ACCESS_FINE_LOCATION) to detect coordinate. So it restarted when a user manually changed the permission in Settings from enabled to disabled. As I understood, MainActivity tries to start, but onCreate doesn't execute. Instead all fragments in FragmentManager begin to create. Because they can access MainActivity (which is still not created), an exception is thrown.

To overcome this exception you should see a stacktrace and find what fragment accesses to it's activity or application in constructor. For instance, you defined a local variable that gets GPS status in a Fragment or ViewModel (it will be calculated in init block).

See also Android 6 Permissions => Crash when disable permission and go back to the app and Activity restart every time when disable permission from setting.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
1

Android will kill the app if certain permissions are changed this is standard android behavior

ICW
  • 4,875
  • 5
  • 27
  • 33