2

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

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
LostGeek
  • 123
  • 13

3 Answers3

3

You need to remove :

super.onBackPressed();
FUBUs
  • 617
  • 5
  • 9
2

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.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
1

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?

Community
  • 1
  • 1
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115