13

I am developing an android app with structure: MainActivity->FragmentA->FragmentB->Fragment C

Now in Fragment C, I go to Permission Setting and make access Camera to Deny, then I return an App. I realize that app restarted (transit to Fragment A). May you give me any reason and suggestion to solve it.

Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29
  • 1
    please add code with your question – Shubham Vala Sep 24 '18 at 13:20
  • 1
    It's intended system behavior, when any permission is revoked app is forcefully killed to prevent any potential runtime errors in place where they were required. – Pawel Sep 24 '18 at 13:20
  • As I explain above when I disable permission from setting then app restart so here there is no need for sample code. – Darshan Mistry Sep 24 '18 at 13:22
  • @Pawel I know that but how to handle this kind of issue. – Darshan Mistry Sep 24 '18 at 13:23
  • @Pawel it depends on the behavior defined in the response to the permission asked. Many applications don't stop just because your denied some runtime permission. Take WhatsApp for example: they ask for message/sms permissions so they can detect verification code on the account linking, when you first run the app. If you deny, it will simply not detect the verification code, and you have to enter it yourself. Darshan Mistry please add some code so it can be clearer for us what the problem is. – miguelarc Sep 24 '18 at 13:23
  • @miguelarc from what I understand OP is not denying the permission - he's revoking it after it was already granted from the app settings activity. – Pawel Sep 24 '18 at 13:24
  • Oh I see. I missunderstood it :) – miguelarc Sep 24 '18 at 13:25
  • bhai fragment no code post kro ne – Tejas Pandya Sep 24 '18 at 13:26
  • Same issue that describes by @Pawel – Darshan Mistry Sep 24 '18 at 13:26
  • @DarshanMistry app being restarted should not be an issue as long as you properly implement fragment and activity `saveInstanceState` methods. You can use developer setting `don't keep activities` to test it. – Pawel Sep 24 '18 at 13:32
  • Is there some documentation on this app restart behaviour officially from Android, when permission is revoked ? I am also facing the same issue but on React Native. – Kunal Chawla Aug 26 '19 at 18:07

2 Answers2

8

When you revoke permissions from app settings - Activity restarts and all it's components too. But in onCreate(...) savedInstanceState doesn't equal to null.

Thus you can use such hack:

if (savedInstanceState != null) {

}

Using the above hack app resume last fragmentC.

Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29
  • 1
    Will it impact on clearing singleton objects values? I am observing same, any solution for persist singleton objects values. – kunal khedkar Oct 04 '21 at 12:12
3

This is something normal and expected in AOSP. Android kills the app process while saving all states and fragments in FragmentManager, so you should retrieve back your fragment from the FragmentManager you were commiting tour fragments to. See an example approach:

// Try to retrieve fragment from fragment manager
ExampleFragment exampleFragment = (ExampleFragment) getSupportFragmentManager().findFragmentByTag("ExampleFragment");
// Fragment was not saved in fragment manager, create a new instance
if (exampleFragment == null) exampleFragment = ExampleFragment.newInstance();

Note that I'm using getSupportFragmentManager() as an example here (in the activity) but you have to use getChildFragmentManager() within fragments.

Sdghasemi
  • 5,370
  • 1
  • 34
  • 42