after disable permissions from settings and return to our app getting crash
Asked Answered
T

3

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!

Tomtoma answered 24/3, 2018 at 11:18 Comment(7)
post your logcatIsosceles
"Observed that app gets created again" - That's what is supposed to happen. You need to account for that possibility when doing anything that requires a dangerous permission.Gerry
If you have Fragment in your activity then you need to get Fragment from Stack and then replace it.Lorraine
Do you have the log or crash report?Obstinacy
is is fine when user accepts the permisson but when user denies it and redirect to current activity app is getting crashedTomtoma
Share your coddeGlisson
Have you been able to fix it, I'm also facing same problem, please update me if you have resolved it.Salvage
M
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();
}
Mcgowan answered 19/4, 2018 at 23:2 Comment(1)
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?Hillell
B
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.

Biscay answered 29/11, 2021 at 12:9 Comment(0)
E
1

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

Emilieemiline answered 1/6, 2022 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.