Callback after orientation change becomes null

android, android-fragments, android-orientation

Solution

When configuration of the `Activity` being destroy and recreates.

When `Configuration` of the phone is changed the method `onConfigurationChange` called.

So you can initialize your `Callbacks` in `onConfigurationChange`

Problem

I have have a `FragmentActivity` with two tabs which are `ListFragment`s. Each `ListFragment` has a callback. An example of a callback The callback is associated inside of the onAttach(...) method ``` OnStatusUpdateListener mStatusUpdateCallback; public interface OnStatusUpdateListener { public void onStatusUpdate(); } @Override public void onAttach(Activity activity) { Log.d(TAG, "onAttach"); super.onAttach(activity); try { mStatusUpdateCallback = (OnStatusUpdateListener)activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnStatusUpdateListener"); } } @Override public void onCreate(Bundle savedInstanceState) { Log.d(TAG, "onCreate"); super.onCreate(savedInstanceState); setRetainInstance(true); } ``` Later on, I communicate with the `FragmentActivity` by this callback which works fine normally. Within the `ListFragment`, I have an `ImageButton` that will call a `DialogFragment` which also has a callback. This callback is implemented in my `ListFragment` and is what triggers the callback that is null ``` public void onStatusOption() { Log.d(TAG, "onStatusOption"); // Update stuff // Here is where mStatusUpdateCallback is null after rotate mStatusUpdateCallback.onStatusUpdate(); } ``` The problem is that if I ever rotate the phone while the application is running, mStatusUpdateCallback becomes null. This of course means I cannot execute the callback. Does anyone know how to fix this? What I've tried According to https://stackoverflow.com/a/6029070/935779 it appears that a new reference to `OnStatusUpdateListener` may have been created so I cannot reference the old, but doesn't offer a solution. I've also tried retaining the state as per https://stackoverflow.com/a/6787393/935779, but I can't save a reference to a callback as far as I can tell. I also would really rather not do the `android:configChanges="orientation|keyboardHidden"` method since that just seems like a hack and my layout changes in landscape. Stacktrace ``` FATAL EXCEPTION: main java.lang.NullPointerException at com.blug.blah.Fragment.StatusFragment.onStatusOption(StatusFragment.java:197) at com.blug.blah.MyActivity.onStatusOption(MyActivity.java:243) at com.blug.blah.Dialog.StatusOptionDialog$1$1.onClick(StatusOptionDialog.java:108) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4627) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) at dalvik.system.NativeStart.main(Native Method) ```

Original source

Related problems