Resultreceiver Doesn't Survire To Screen Rotation
Solution 1:
No,
android:configChanges="orientation"is not a solution.
To use ResultReceiver I:
save it on orientation changes:
@OverridepublicvoidonSaveInstanceState(Bundle outState) { outState.putParcelable(Consts.RECEIVER, mReceiver); super.onSaveInstanceState(outState); }reset the receiver:
@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (savedInstanceState != null) { mReceiver = savedInstanceState.getParcelable(Consts.RECEIVER); } else { mReceiver = newMyResultReceiver(newHandler()); } mReceiver.setReceiver(this); }
Here is my ResultReceiver class:
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
publicclassMyResultReceiverextendsResultReceiver {
private Receiver mReceiver;
publicMyResultReceiver(Handler handler) {
super(handler);
}
publicvoidsetReceiver(Receiver receiver) {
mReceiver = receiver;
}
publicinterfaceReceiver {
publicvoidonReceiveResult(int command, Bundle resultData);
}
@OverrideprotectedvoidonReceiveResult(int command, Bundle resultData) {
if (mReceiver != null) {
mReceiver.onReceiveResult(command, resultData);
}
}
}
Solution 2:
I am using a BroadcastReceiver registered using LocalBroadcastManager and it is working properly. It wasn't so simple. Does a better solution exist?
Solution 3:
I think I stumbled upon the same issue and resolved it by verifying for NULL in the onReceivedResult method of my ResultReceiver. The code posted here works on a worker fragment (fragment without UI and setRetainInstance(true) in onCreate)
protectedvoidonReceiveResult(int resultCode, Bundle resultData) {
//Verify activityif(getActivity() != null){
//Handle result
}else{
notificationPending = true;
}
}
The notificationPending flags helps the fragment hold the pending notification if the activity was not found (Activity is not available on fragment Detach).
When the fragment reattaches to the activity i perform this logic
publicvoidonAttach(Activity activity){
super.onAttach(activity);
if(notificationPending){
//Handle notification
notificationPending = false;
}
}
Hope it helps. You can ask for further details if you like. Cheers
Solution 4:
Yes, this normal since the ResultReceiver might be "headless".
I tried saving the ResultReceiver at onSaveInstanceState(), but it didn't work, since updates, that happen while the receiving Fragment is destroyed, get lost, and the references to callbacks too.
An explanation and a possible solution can be found here: https://stanmots.blogspot.com/2016/10/androids-bad-company-intentservice.html
Another good read concerning this problem: https://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html
My full solution how to use a ResultReceiver can be found here:
https://stackoverflow.com/a/54334864/6747171
Solution 5:
The getActivity() returns null. Is this normal?
Android Activities are recreated after device rotation.
After activity is recreated it does not holds old context.that's why your getting getActivity() as null
What approach could I use to allow notification even on screen rotation? Local Broadcast?
If you dont want activity to recreated on screen rotation.mention following in manifest
<activityandroid:name=".MyActivity"android:configChanges="orientation" <<<<<<<<<
android:label="@string/app_name"android:screenOrientation="portrait" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity>And last You will have to override following in Activity.
@OverridepublicvoidonConfigurationChanged(Configuration newConfig)
{
// TODO Auto-generated method stubsuper.onConfigurationChanged(newConfig);
}
Post a Comment for "Resultreceiver Doesn't Survire To Screen Rotation"