6

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)
Community
  • 1
  • 1
Kirk
  • 16,182
  • 20
  • 80
  • 112
  • 1
    initialize `callbacks` in `onConfigurationChange` method. – Mohsin Naeem Dec 01 '12 at 03:18
  • Its really hard to tell what is going on from the code you posted. In theory that shouldn't happen. I have similar setups and I have not seen nullpointers. Can you post the logcat? – Marco RS Dec 01 '12 at 03:23
  • @MMohsinNaeem See the code first :) – Ali Imran Dec 01 '12 at 03:28
  • @Marco I've added a spot where I call it from as well as the stacktrace. – Kirk Dec 01 '12 at 03:41
  • hmmm its still hard to tell whats going on. As an alternative you can use the LocalBroadcastManager that comes with the support library to pass data/messages(http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager). Or you can also use an EventBus library such as Otto or EventBus by GreenDroid. – Marco RS Dec 01 '12 at 03:51
  • @MMohsinNaeem If I handle `onConfigurationChange` and initialize the callbacks there too, it solves this problem. If you will put your suggestion as an answer, I'll accept it. (Creates a new problem, but you solved my issue in this question) – Kirk Dec 01 '12 at 17:33

3 Answers3

2

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

Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
1

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

Hadi
  • 91
  • 1
  • 9
0

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.

Jug6ernaut
  • 8,219
  • 2
  • 26
  • 26