Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8789333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:22:40+00:00 2026-06-13T22:22:40+00:00

I came across the following memory leak example package com.justinschultz.android; import android.app.Activity; import android.app.AlertDialog;

  • 0

I came across the following memory leak example

package com.justinschultz.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class LeakedDialogActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setMessage("This dialog leaks!").setTitle("Leaky Dialog").setCancelable(false).setPositiveButton("Ok", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which) {}
        });

        AlertDialog alert = builder.create();
        alert.show();
    }
}

I don’t understand why it leaks on a rotation. I understand, a new Activity is created while the dialog is still on screen (containing a reference to the old activity). Suppose you close the dialog and rotate again. Shouldn’t the reference to the oldest activity be gone, thus allowing the memory to be reclaimed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:22:42+00:00Added an answer on June 13, 2026 at 10:22 pm

    AlertDialogs (if used outside of Fragments) should be instantiated through onCreateDialog()/showDialog() to avoid leaks.

    This implementation is deprecated and should be replaced by a DialogFragment, but will work for you:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        showDialog(YOUR_DIALOG_ID);
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch(id) {
        case YOUR_DIALOG_ID:
            return new AlertDialog.Builder(LeakedDialogActivity.this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setMessage("This dialog leaks!")
            .setTitle("Leaky Dialog")
            .setCancelable(false)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {}
            })
            .create();
        }
        return super.onCreateDialog(id);
    }
    

    ADDED

    When you don’t create a dialog in onCreateDialog its essentially not attached to (or owned by) the Activity, and therefore, it’s lifecycle. When the Activity is destroyed or recreated, the Dialog maintains a reference to it.

    In theory, the Dialog should not leak if you use setOwnerActivity() and dismiss in onPause() (I believe).

    I’m not sure you have to worry much about this gotcha as far as general leaks go. Dialogs are kind of a special case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across the following http://ckeditor.com/demo , and was wondering if anyone had a
I came across the following URL today: http://www.sfgate.com/cgi-bin/blogs/inmarin/detail??blogid=122&entry_id=64497 Notice the doubled question mark at
I believe I came across a memory leak in IE7. One of our pages
I came across the following markup in a JSP file in a legacy app
While reading Bruce Eckel i came across the following example: #include <cstdio> #include <cstdlib>
I was solving some simple pointer exercises when i came across the following example:
I came across following code and don't know what does having from twice mean
I came across the following code : int i; for(; scanf(%s, &i);) printf(hello); As
I came across the following question : How many times will the following for
I came across the following expression in someone else's code. I think it's terrible

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.