Sai
Sai

Reputation: 41

Android AlertDialog: window leaked error

This error is showing for alert.show() line of code .In click interface of dialog, starting new activity like this startActivity(new Intent(LayoutActivity.this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

Tried with closing of alert.dismiss() in onPause() and onStop() and onDestroy() still getting same error

Upvotes: 0

Views: 1437

Answers (4)

lomza
lomza

Reputation: 9716

I had the same error. And what I've found out is that it will be enough to cancel the alert dialog in onStop(). Try this peace of code in onStop(): if(dialog != null) dialog.cancel();

Upvotes: 2

Tiko
Tiko

Reputation: 1000

Your Activity where this dialog should be seen is not active , or maybe in manifest you set a property noHistory of current activity to true

Upvotes: 0

JohnCookie
JohnCookie

Reputation: 671

why addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) it will close all Acitivties opened before. If you have pretend the activity's theme to be a dialog, just use it as an Activity. If there is an AlertDialog Object, then call alert.dismiss().

Upvotes: 0

lulumeya
lulumeya

Reputation: 1628

that is android's typical problem.

the goal to solve the problem is, show or hide dialog between activity's on Resume() and onPause()

like this

boolean isShown = false;
public void onResume()
{
   isShown = true;
   ...
}

public void onPause()
{
   isShown = false;
   ...
}

public void showDialog()
{
   if(isShown)
   {
      ...
   }
}

Upvotes: 1

Related Questions