ResultReceiver doesn't survire to screen rotation
No,
android:configChanges="orientation"
is not a solution.
To use ResultReceiver I:
save it on orientation changes:
@Override public void onSaveInstanceState(Bundle outState) { outState.putParcelable(Consts.RECEIVER, mReceiver); super.onSaveInstanceState(outState); }
reset the receiver:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (savedInstanceState != null) { mReceiver = savedInstanceState.getParcelable(Consts.RECEIVER); } else { mReceiver = new MyResultReceiver(new Handler()); } mReceiver.setReceiver(this); }
Here is my ResultReceiver class:
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
public class MyResultReceiver extends ResultReceiver {
private Receiver mReceiver;
public MyResultReceiver(Handler handler) {
super(handler);
}
public void setReceiver(Receiver receiver) {
mReceiver = receiver;
}
public interface Receiver {
public void onReceiveResult(int command, Bundle resultData);
}
@Override
protected void onReceiveResult(int command, Bundle resultData) {
if (mReceiver != null) {
mReceiver.onReceiveResult(command, resultData);
}
}
}