Tri Le
Tri Le

Reputation: 61

android.widget.PopupWindow$PopupDecorView leak when rotate device with menu open

I started Android development a few weeks ago and went through creating menu in Activity and read up about Activity life cycle. I kept running into this memory leak error when I have the pop up menu open and rotate the device.

I originally had this problem on a bigger project but to make it simple for recreating the problem, one can follow the steps:

com.example.myapplication E/WindowManager: android.view.WindowLeaked: Activity com.example.myapplication.MainActivity has leaked window android.widget.PopupWindow$PopupDecorView{36b6390 V.E...... ......ID 0,0-588,144} that was originally added here ...

I know this related to the Activity went through onPause(), onStop() and onDestroyed() during the device rotation process and PopupWindow$PopupDecorView did not get closed. In a sense it is similar the question here [PopupMenu PopupWindow$PopupViewContainer leak] (PopupMenu PopupWindow$PopupViewContainer leak) However I can't figure out how to solve it since I don't have a reference to the menu in those life cycle methods to have it dismissed.

My other question is that Should Android be take care of this automatically be cause menu is widely used? I went through several tutorials on how to create menu everybody show how the menu can be created without mentioning the above problem.

Hope somebody can give me the lead to solve this annoying problem or at least how to close down the pop up menu in one of the life cycle method.

MainActivity.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onPause() {
    super.onPause();
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.myapplication.MainActivity">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />
</menu>

Upvotes: 6

Views: 2497

Answers (1)

Preetika Kaur
Preetika Kaur

Reputation: 2031

Add

android:configChanges="orientation|screenSize"

in <activity> tag in manifest file that error will not occur. It works for me try it and let me know.

Upvotes: -5

Related Questions