2

Sorry if this been asked before but I couldn't find an answer to my specific case. Also sorry that I'm new and a little stupid.

Problem: I'm showing a dialog from a fragment and passing along a context in my constructor method because I need a context in my dialog to register for broadcastrecievers etc.

DialogFragment fragmentDialog = MyDialog.myConstructor(getActivity());
fragmentDialog.show(getFragmentManager(), "dialog");

Then in MyDialog class I store the context in a instance variable. The problem arises when rotating the device and I get a nullPointerException when I try to use the context again in the dialog.

Can this be solved in some easy way?

Toydor
  • 2,277
  • 4
  • 30
  • 48

4 Answers4

2

If the device is rotated the Activity will be destroyed and recreated. So the Context you passed to your Fragment points on the Activity which was destroyed.

You could use setRetainInstance(true) in your Fragment. This way your Fragment will survive the recreation of the Activity.

To solve the NPE you have to pass the Context to the Fragment, if the Activity is recreated. Then the Context belongs to the new Activity.

In fact, without this update every line of code which points on the Activity like getActivity() or getFragmentManager() will lead in a NPE.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • If I only needed the context in the creation of the dialog setRetainInstance(true) could work. Correct me if I'm wrong but after the rotation the context still be null even if I call setRetainInstance(true). I tried to use getActivity() in my DialogFragment class, but it didn't work. The last thing you said is what I think I have to do. Update context in the dialog from my parent fragment class everytime the device rotates. Thanks! – Toydor Jul 25 '13 at 22:14
  • setRetainInstance(true) makes the dialog dismissed on rotation – Zain Sep 06 '19 at 20:10
1

You get the NullPointerException because activites are destroyed and recreated when rotating the screen.

The SO post below gives more info...

https://stackoverflow.com/a/1673374/

Community
  • 1
  • 1
Jim
  • 6,753
  • 12
  • 44
  • 72
1

Please be careful with the order of events if you rotate a FragmentActivity, because this can also be a source of NullPointerExceptions.

This is not documentated: When the FragmentActivity is created the first time,

public class MyActivity extends FragmentActivity implements
                           MyFragment.OnFragmentInteractionListener {

    private int var1;
    private int var2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         //before
         var1 = 3;       
         super.onCreate(Bundle savedInstanceState) 
         //after
         var2 = 5;
    }

    //Interface Methods
    public int getVar1() { return var1; }
    public int getVar2() { return var2; }
}

both of the [before] and [after] code will be run before the fragments are attached and created. So, if you get the vars in the onCreate() call of the Fragment you get both vars. But when you rotate your device, the Activity is recreated from the savedInstanceState in the super call. Now, the fragments are reattached and created anew in this call! That means, this time the Methods of the Listener Interface are called before your [after] code. So, if you pass the Context of the activity to the fragment and get Information through the Interface like it is shown in: https://developer.android.com/training/basics/fragments/communicating.html

you get a NullPointerException for var2 because the interface methods are called from the fragments onCreate() onAttach() ... functions before the [after] code in the Activity's onCreate() is executed! So, take care that you set your Information the InterfaceFunctions are accessing before the super call.

0

Depending on what you're doing in your initialization you could consider creating a new class that extends Application and moving your initialization code into an overwridden onCreate method within that class.

public class MyApplicationClass extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    // TODO Put your application initialization code here.
  }
}

And you are not stupid, even experts need help from time to time.

NightSkyCode
  • 1,141
  • 2
  • 16
  • 33