Kirk
Kirk

Reputation: 16245

Callback after orientation change becomes null

I have have a FragmentActivity with two tabs which are ListFragments. 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)

Upvotes: 6

Views: 3049

Answers (3)

Hadi
Hadi

Reputation: 91

First you can put this code into manifest tag and force activity to save your callback android:configChanges="orientation|screenSize" and Second you can use onConfigurationChange method to initialize your callback

Upvotes: 1

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

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

Upvotes: 2

Jug6ernaut
Jug6ernaut

Reputation: 8325

By default when your application changes orientation that whole activity is destroied and created. Because of this(and without more of your code posted) it can be assumed that your references are going null as the activity is destroyed and need to be reset when the activity is recreated.

Alternatively you can specify in your manifest that your activity handle orientation changes and then the activity will not be destroyed when orientation changes.

Upvotes: 0

Related Questions