Kindle Fire: PopupMenu$OrientationChangeListener leak
Asked Answered
O

1

0

I have a little popup menu that anchors on a button in the action bar. This works well on 3.0/3.1 Xoom and Galaxy tabs, and a 4.1 Nexus 7.

However, on a 7" Fire HD (this one), I get an error about a leaked intent receiver when exiting the application. The error occurs only if the menu was not opened during that run.

There is no mention of OrientationChangeListener in the one copy of the source I've found; I suspect Amazon has a different implementation.

Questions:

  • Has anyone encountered this?
  • Does anyone know of a workaround or a fix?
  • (Where) can I find Amazon's source code?
  • Finally, (shudder) how important is it that I don't leak a receiver on application exit?

Here is the menu xml:

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_profile"
        android:title="@string/menu_item_profile" 
        ></item>
    <item android:id="@+id/menu_logout"
        android:title="@string/menu_item_logout"
        ></item>

</menu>

This is where I register it:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);

    MenuItem login = menu.findItem(R.id.menu_login);
    Button button = (Button) login.getActionView().findViewById(R.id.login);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            profileMenu.show();
        }
    });


    // profileMenu is an instance field
    profileMenu = new PopupMenu(this, button);
    inflater.inflate(R.menu.profile_menu, profileMenu.getMenu());
    profileMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            // there was code here, but I removed it all and the problem persists
            return false;
        }
    });

    return true;
}

Here is the full stack trace:

10-21 20:55:28.461: E/ActivityThread(4526): Activity **.app.ListActivity has leaked IntentReceiver android.widget.PopupMenu$OrientationChangeListener@422d77e0 that was originally registered here. Are you missing a call to unregisterReceiver()? 
10-21 20:55:28.461: E/ActivityThread(4526): android.app.IntentReceiverLeaked: Activity **.app.ListActivity has leaked IntentReceiver android.widget.PopupMenu$OrientationChangeListener@422d77e0 that was originally registered here. Are you missing a call to unregisterReceiver()? 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:826) 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:621) 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1072) 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1059) 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1053) 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:357) 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.widget.PopupMenu.<init>(PopupMenu.java:81) 
10-21 20:55:28.461: E/ActivityThread(4526):     at **.app.ListActivity.onCreateOptionsMenu(ListActivity.java:350) 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.app.Activity.onCreatePanelMenu(Activity.java:2558) 
10-21 20:55:28.461: E/ActivityThread(4526):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:398) 
10-21 20:55:28.461: E/ActivityThread(4526):     at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:883) 
10-21 20:55:28.461: E/ActivityThread(4526):     at com.android.internal.policy.impl.PhoneWindow$2.run(PhoneWindow.java:3008) 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.os.Handler.handleCallback(Handler.java:605) 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.os.Handler.dispatchMessage(Handler.java:92) 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.os.Looper.loop(Looper.java:137) 
10-21 20:55:28.461: E/ActivityThread(4526):     at android.app.ActivityThread.main(ActivityThread.java:4491) 
10-21 20:55:28.461: E/ActivityThread(4526):     at java.lang.reflect.Method.invokeNative(Native Method) 
10-21 20:55:28.461: E/ActivityThread(4526):     at java.lang.reflect.Method.invoke(Method.java:511) 
10-21 20:55:28.461: E/ActivityThread(4526):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
10-21 20:55:28.461: E/ActivityThread(4526):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
10-21 20:55:28.461: E/ActivityThread(4526):     at dalvik.system.NativeStart.main(Native Method)
Overabundance answered 22/10, 2012 at 4:58 Comment(0)
O
1

I've solved this particular problem by just creating the PopupMenu when its anchor button is first pressed. Since the leak only occurred when exiting the activity before showing the menu, I figured eliminating that state was a decent option.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);

    MenuItem login = menu.findItem(R.id.menu_login);
    final Button button = (Button) login.getActionView().findViewById(R.id.login);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View item) {
            if (profileMenu == null) {
                profileMenu = new PopupMenu(ListActivity.this, button);
                inflater.inflate(R.menu.profile_menu, profileMenu.getMenu());
                profileMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        // do something with item
                        return false;
                    }
                });
            }
            profileMenu.show();
        }
    });
    return true;
}

There is one remaining edge case that causes the PopupMenu's window to be leaked. I'll post that in a new question and link it here shortly. I don't think I'll solve that one without reimplementing PopupMenu from scratch, though.

Overabundance answered 24/10, 2012 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.