ResultReceiver does not withstand screen rotation

I am using a REST client in Android. I saw an example of using Service to connect to the server and ResultReceiver to notify that the operation was completed. I call the service from a fragment, and if I try to rotate the screen while the service is running, the getActivity () method in ResultReceiver returns null, because maybe this fragment is no longer in the layout.

Fragment callback method:

 @Override public void onReceiveResult(int resultCode, Bundle resultData) { Response response = (Response) resultData .getSerializable(RestService.RESULT); if (resultCode == RestService.SUCCESS && response != null) { if (getActivity() != null) { recommendationResponse = response; getLoaderManager().restartLoader(0, new Bundle(), Fragment.this); } } } 

getActivity() returns null. This is normal? What approach can be used to enable notification even when the screen is rotated? Local broadcast?

+5
android rest service
Jun 07 '12 at 7:16
source share
5 answers

I am using BroadcastReceiver registered with LocalBroadcastManager and it works correctly. It was not so simple. Is there a better solution?

+1
Jun 07 2018-12-12T00:
source share

No,

 android:configChanges="orientation" 

not a solution.

To use ResultReceiver I:

  • save it when changing orientation:

     @Override public void onSaveInstanceState(Bundle outState) { outState.putParcelable(Consts.RECEIVER, mReceiver); super.onSaveInstanceState(outState); } 
  • reset 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); } } } 
+12
Apr 14 '14 at 11:47
source share

I think I came across the same problem and resolved it by checking NULL in the onReceivedResult method of my ResultReceiver. The code posted here works with a working snippet (a snippet without a user interface and setRetainInstance (true) in onCreate)

 protected void onReceiveResult(int resultCode, Bundle resultData) { //Verify activity if(getActivity() != null){ //Handle result }else{ notificationPending = true; } } 

The notificationPending flags help the fragment hold the pending notification if no activity has been detected (activity is not available when the fragment is disconnected).

When a fragment is attached to activity again, I execute this logic

 public void onAttach(Activity activity){ super.onAttach(activity); if(notificationPending){ //Handle notification notificationPending = false; } } 

Hope this helps. You can request additional information if you wish. Greetings

+1
Sep 29 '12 at 19:10
source share

Yes, this is normal, as the ResultReceiver may be "headless".

I tried to save the ResultReceiver in onSaveInstanceState() , but this did not work, because the updates that occur when the received Fragment destroyed are lost and callback links too.

The explanation and possible solution can be found here: https://stanmots.blogspot.com/2016/10/androids-bad-company-intentservice.html

Another useful article about this issue: https://www.androiddesignpatterns.com/2013/04/retainment-objects-across-config-changes.html

My complete solution on how to use the ResultReceiver can be found here: https://stackoverflow.com/a/22023039/ ...

0
Jan 23 '19 at 20:30
source share

GetActivity () returns null. This is normal?

Android actions are recreated after the device is rotated.

After the activity is recreated, it does not contain the old context. This is why you get getActivity() as null

What approach can be used to enable notification even on screen rotation? Local broadcast?

If you do not want the activity recreated on the rotation.mention screen in the manifest

  <activity android:name=".MyActivity" android:configChanges="orientation" <<<<<<<<< android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

And last, you need to override the following steps.

 @Override public void onConfigurationChanged(Configuration newConfig) { // TODO Auto-generated method stub super.onConfigurationChanged(newConfig); } 
-one
Jun 07 2018-12-12T00: 00Z
source share



All Articles