Reputation: 3020
I am overriding the back button . i want to show user a confirmation dialog when leaving my application. while creating the dialog i got the following error
Activity com.example.netlogger.ActivityLogin has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@49ffa7b8 that was originally added here
04-16 16:47:40.885: E/WindowManager(7757): android.view.WindowLeaked: Activity com.example.netlogger.ActivityLogin has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@49ffa7b8 that was originally added here
04-16 16:47:40.885: E/WindowManager(7757): at android.view.ViewRoot.<init>(ViewRoot.java:247)
04-16 16:47:40.885: E/WindowManager(7757): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
04-16 16:47:40.885: E/WindowManager(7757): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-16 16:47:40.885: E/WindowManager(7757): at android.view.Window$LocalWindowManager.addView(Window.java:424)
04-16 16:47:40.885: E/WindowManager(7757): at android.app.Dialog.show(Dialog.java:241)
04-16 16:47:40.885: E/WindowManager(7757): at com.example.netlogger.ActivityLogin.onBackPressed(ActivityLogin.java:130)
04-16 16:47:40.885: E/WindowManager(7757): at android.app.Activity.onKeyUp(Activity.java:1888)
04-16 16:47:40.885: E/WindowManager(7757): at android.view.KeyEvent.dispatch(KeyEvent.java:1063)
04-16 16:47:40.885: E/WindowManager(7757): at android.app.Activity.dispatchKeyEvent(Activity.java:2068)
04-16 16:47:40.885: E/WindowManager(7757): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1703)
04-16 16:47:40.885: E/WindowManager(7757): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2473)
04-16 16:47:40.885: E/WindowManager(7757): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2443)
04-16 16:47:40.885: E/WindowManager(7757): at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
04-16 16:47:40.885: E/WindowManager(7757): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 16:47:40.885: E/WindowManager(7757): at android.os.Looper.loop(Looper.java:123)
04-16 16:47:40.885: E/WindowManager(7757): at android.app.ActivityThread.main(ActivityThread.java:4633)
04-16 16:47:40.885: E/WindowManager(7757): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 16:47:40.885: E/WindowManager(7757): at java.lang.reflect.Method.invoke(Method.java:521)
04-16 16:47:40.885: E/WindowManager(7757): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-16 16:47:40.885: E/WindowManager(7757): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-16 16:47:40.885: E/WindowManager(7757): at dalvik.system.NativeStart.main(Native Method)
My code is the following
@Override
public void onBackPressed() {
super.onBackPressed();
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
if (!prefs.getBoolean("ask_for_exit", false)) {
exitAlertDialog = new Builder(ActivityLogin.this)
.setTitle("Are You Sure?")
.setMessage("Are you sure you want to exit")
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
finish();
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
exitAlertDialog.dismiss();
}
}).create();
exitAlertDialog.show();
}
}
What's this error means? if there is any clue please let me know. Thanks!
Upvotes: 0
Views: 110
Reputation: 8281
The problem is at super.onBackPressed();
delete that line.
super.onBackPressed();
exits the app. you are trying to add a alert dialoge after it. Thats the problem.
Upvotes: 0
Reputation: 6441
Remove super.onBackPressed();
from the function, when use that, Activity automatiacally calls onStop();
Upvotes: 0
Reputation: 34265
If you call
super.onBackPressed();
from your onBackPressed()
method then default back button behavior will happen, and your activity will be destroyed without cleaning (or showing) up your AlertDialog
. You should comment out that line.
Also if your SharedPreference is to not show AlertDialog, then you should explicitly call finish()
in an else
block.
if (!prefs.getBoolean("ask_for_exit", false)){
//show alert
}else{
finish();
}
Upvotes: 2