Callback after orientation change becomes null

拥有回忆 提交于 2019-12-10 17:20:29

问题


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)

回答1:


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/13655944/callback-after-orientation-change-becomes-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!