-1

I have registered SMS_RECIEVED broadcastreceiver in CustomWebView. I've taken care of registering and unregistering of receiver on activity's life cycle. All works well until orientation changes.

I've tried placing register/unregister reciever under different states of webview like onAttachedToWindow()/onDetachedFromWindow(), onPause()/onResume()(explicitly calling from activity's onPause()/onResume()), onStart()/onStop(). But nothing seems to prevent IntentReceiver leak on orientation change.

Here's the code i've tried:

CustomWebView.java

public class CustomWebView extends WebView {

    BroadcastReceiver reciver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                final Bundle bundle = intent.getExtras();
                try {
                    if (bundle != null) {
                        final Object[] pdusObj = (Object[]) bundle.get("pdus");
                        Log.v(tag, "Got PDUS Obj [" + pdusObj + "]");
                        }
                    }

                } catch (Exception e) {
                    Log.e(tag, "Exception smsReceiver" + e);
                }
            }
        };

    --------------------
    --------------------
    some code
    --------------------

    @Override
    public void onResume() {
        try {
            IntentFilter intents = new IntentFilter(
                    "android.provider.Telephony.SMS_RECEIVED");
            getContext().registerReceiver(reciver, intents);
            Log.i(tag, "Webview sms reciever registered");
        } catch (Exception e) {
            e.printStackTrace();
        }

        super.onResume();
    }

    @Override
    public void onPause() {
        try {
            getContext().unregisterReceiver(reciver);
            Log.i(tag, "Webview sms reciever unregistered");
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onPause();
    }



    // Tried this one too
    /* @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.i(tag, "Billdesk onAttach to window called");
        try {
            IntentFilter intents = new IntentFilter(
                    "android.provider.Telephony.SMS_RECEIVED");
            getContext().registerReceiver(reciver, intents);
            Log.i(tag, "Webview sms reciever registered");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.i(tag, "Billdesk onDetach From window to window called");
        try {
            getContext().unregisterReceiver(reciver);
            Log.i(tag, "Webview sms reciever unregistered");
        } catch (Exception e) {
            e.printStackTrace();
        }

    } */

}

MainActivity.java

public class MainActivity extends FragmentActivity{

-----------
-----------
-----------

@Override
    protected void onPause() {
        if (this.customWebView != null)
            this.customWebView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        if (this.customWebView != null)
            this.customWebView.onResume();
        super.onResume();
    }

}

I get following error when, orientation is changed :

Activity com.abcd.xyz.ActivityClass has leaked IntentReceiver com.abcd.xyz.CustomWebView$2@4898758 that was originally registered here. Are you missing a call to unregisterReceiver()?

Thanks in advance.

Bhupesh
  • 437
  • 1
  • 9
  • 20
  • At which line getting issue? – ρяσѕρєя K Jun 01 '16 at 10:22
  • Why don't you create methods like registerReceiver/unregisterReceiver in CustomWebView and then call it from parent onResume/onPause ... also is the CustomWebView part of the Activity's layout or Fragment layout? – Selvin Jun 01 '16 at 10:34
  • @ρяσѕρєяK I'm getting the error while calling getContext().registerReceiver(reciver, intents); on CustomWebView after orientation change – Bhupesh Jun 01 '16 at 11:12
  • @Selvin I've tried that too. But didn't got the problem solved. CustomWebView is part of fragment layout. – Bhupesh Jun 01 '16 at 11:18

1 Answers1

0

This problem occur due to device screen rotation...

When the phone rotates and the screen changes orientation, Android usually destroys your application’s existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

And your receiver was register on activity's last object or instance. So the solution is save the receiver object...

You can save any Object by Overriding public Object onRetainNonConfigurationInstance () and calling getLastNonConfigurationInstance() in your onCreate method.

@Override
public Object onRetainNonConfigurationInstance() {
// Save the reciever obj here

return data;
}

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     data = getLastNonConfigurationInstance();
}

Hope this help you.

Vishal Chauhan
  • 922
  • 1
  • 6
  • 11