0

I have a DialogFragment to display a list of options. So, I try to do several tests on my apps. I found a problem when changing phone system language in the phone settings while beforehand the DialogFragment is opening in my apps then go back into my apps. It caused a null pointer exception on the DialogFragment.

Basically, the repro is like this :

  1. Opening an activity
  2. The activity will open DialogFragment after some user actions
  3. The user exits the application and enters the phone setup
  4. The user changes the phone system language and then go back to the apps
  5. The DialogFragment still appears, but not in a proper format.
  6. The user presses the back button and the apps will crash automatically.

I've been trying several examples and solutions, but still the apps will crash after the repro stated above.

What should I do to tell the activity to close the DialogFragment when the user changes the phone system language? Do I need to put Flags? but How?

Here's the log :

java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.MainDialogFragment$MainDialogListener.onDestroyMainDialogFragment()' on a null object reference
        at com.example.MainDialogFragment.onDestroy(MainDialogFragment.java:152)

Before changing the phone system language : enter image description here

After changing the phone system language and go back to the apps, the dialogfragment still appears, then the user presses the back button : enter image description here

My Line 152 is :

@Override
public void onDestroy() {
    myMainListener.onDestroyMainDialogFragment();
    myMainListener = null;
    super.onDestroy();
}

Here's my full code : https://codeshare.io/DZZRWO

Thanks in any advance

XVallerie
  • 141
  • 2
  • 11
  • So why does your DialogFragment not appear 'in the proper format'. Why does your app crash when you press the back button? Your first priority should be to fix those problems, but you've included no details on your DialogFragment code or the stack trace of the crash you're experiencing. – ianhanniballake Jul 12 '21 at 04:07
  • @ianhanniballake I've edited my description , please kindly take a look, Thank you – XVallerie Jul 12 '21 at 04:23
  • So where are you populating your list of options (and why isn't doesn't that list exist after process death and recreation)? How are you setting up your `myMainListener` (and why isn't that set correctly after process death and recreation)? Have you done any testing with the "Don't keep activities" developer option, which is specifically available to test these cases? – ianhanniballake Jul 12 '21 at 04:31
  • @ianhanniballake I have set a listener both when the fragment is created and when the fragment is already in the onDestroy stage. I think that the flow becomes like this: Activity Open -> OnCreateDialog -> Dialog appears -> Change Phone Language Settings -> previous init disappeared -> recreate fragment -> but becomes null. Please take a look at my full code : https://codeshare.io/DZZRWO – XVallerie Jul 12 '21 at 04:55

1 Answers1

0

I've found several solutions that I thought would be fit to my case :

  1. Checking and detecting whether the phone system settings have changed, then do something to dismiss the dialog fragment.
  2. adding

setRetainInstance(true)

inside the method onCreateDialog / onStart / setDialogContent (Depends on the case)

XVallerie
  • 141
  • 2
  • 11