LostGeek
LostGeek

Reputation: 123

popupwindow : window get leaked on back button

I am creating popupwindow as below,

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
        LinearLayout linearLayout = new LinearLayout(TimerGameActivity.this);
        TextView textView = new TextView(TimerGameActivity.this);
        textView.setText("Quit? Score will be lost....");
        linearLayout.addView(textView);
        PopupWindow popupWindow = new PopupWindow(linearLayout, 200, 100,true);
        popupWindow.showAtLocation(linearLayout, Gravity.BOTTOM, 10, 10)
}

But I am facing problem as below. It doesn't let me show popupwindow and destroys activity when i press back button, it gives me the following error:

04-18 15:04:55.457: E/WindowManager(590): Activity has leaked window android.widget.LinearLayout@44f88be8 that was originally added here

Help me out. Thanks

Upvotes: 2

Views: 1695

Answers (3)

Lalit Poptani
Lalit Poptani

Reputation: 67296

It is obvious to get an exception there, because when you are pressing Back Button super.onBackPressed(); is fired and your Activity is going to get finish and at the same time you are trying to show a PopupWindow. So, there is no UI for PopupWindow to be shown. So, just remove super.onBackPressed(); and try to show PopupWindow instead.

Upvotes: 2

onBackPressed() will going to destroy your activity and at the same time you doing UI operation with reference of the same activity, so how it will work?

reference Which actions does the back button/back key on Android trigger?

Upvotes: 1

FUBUs
FUBUs

Reputation: 617

You need to remove :

super.onBackPressed();

Upvotes: 3

Related Questions