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 8410899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:17:59+00:00 2026-06-10T00:17:59+00:00

I know that creating static methods to create AlertDialogs is not a good sign.

  • 0

I know that creating static methods to create AlertDialogs is not a good sign. But, whenever I felt like creating some AlertDialogs, I always have to place them inside an Activity subclass. I’ve been looking around in SO, trying to find a good way to factor the code, so that I don’t have to initialize and create AlertDialogs from an Activity subclass.

Here is an example of my code, designed in such a way that I have to sacrifice performance speed for AlertDialogs, which is very necessary in my project plans.

public void onCreate(Bundle b) {
    super.onCreate(b);
    accelerometer = new Accelero();
    leaderboard = new Score(this);
    renderView = new RenderView(this);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(renderView);

    // TODO: Refactor this, to speed things up.
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    input = new EditText(this);
    builder.setView(input);
    builder.setTitle("Enter Name for High Score!");
    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // TODO: Polish the dialog.
            // TODO: Add a method of obtaining the score from RenderView.
            renderView.getStage().reset();
            renderView.setDialogFlag(false);
        }
    });
    builder.setNegativeButton("Back", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            PlayActivity.this.onBackPressed();
        }
    });
    renderView.setLosingDialog(builder.create());
    builder = new AlertDialog.Builder(this);
    builder.setTitle("You win!");
    builder.setPositiveButton("Next Stage", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            renderView.createStage(getAssets(), stageNumber);
            renderView.pauseGame();
        }
    });
    renderView.setWinningDialog(builder.create());
    builder = new AlertDialog.Builder(this);
    builder.setTitle("Game Paused!");
    builder.setPositiveButton("Back to Game", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            renderView.unpauseGame();
        }
    });
    builder.setNeutralButton("Restart", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            renderView.resetGame();
        }
    });
    builder.setNegativeButton("Main Menu", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // TODO: Will probably improve some more on this.
            PlayActivity.this.finish();
        }
    });
    renderView.setPausingDialog(builder.create());
}

This isn’t what I wanted. I tried placing them in a new thread to run, but it leaks memory, so it didn’t work. Also, due to issues (Static AlertDialog methods will leak memory, etc.), I just don’t have any other ideas on fixing this.

I don’t know what to do next. So, may I ask, who else have a better way of initializing AlertDialogs without sacrificing performance speeds? Thanks in advance.

  • 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-10T00:18:01+00:00Added an answer on June 10, 2026 at 12:18 am

    First thing I would do is to add some performance analysis instrumentation (measurements) to determine where the code is spending its time. Nothing is more painful than spending time trying to optimize something that doesn’t need optimization 😉

    From looking at this I can see at least one simple optimization: This code creates 6 instances of 6 different anonymous classes just to handle the onClick() callbacks. IMHO that isn’t necessary. You could just use this as the callback interface and make sure that your activity implements DialogInterface.OnClickListener. Then write a single method in your activity that handles all the click events:

    public void onClick(DialogInterface dialog, int which) {
        if (dialog == renderView.getLosingDialog()) {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                // TODO: Polish the dialog.
                // TODO: Add a method of obtaining the score from RenderView.
                renderView.getStage().reset();
                renderView.setDialogFlag(false);
            } else if (which == DialogInterface.BUTTON_NEGATIVE) {
                PlayActivity.this.onBackPressed();
            }
        } else if dialog == renderView.getWinningDialog()) {
            // etc...
        } else if dialog == renderView.getPausingDialog()) {
            // etc...
        }
    }
    

    I can’t guarantee that this will improve performance, but it will definitely make the garbage collector very happy 🙂

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

Sidebar

Related Questions

To learn backbone Im creating a Twitter like app. So you know that Twitter
I know that attributes are not supported inside the method body but i wanted
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that Java have its own garbage collection, but sometimes I want to
I'm sure this is something that comes up regularly, but I don't know how
I know that it is an easy task, but after changing my code it
I'm creating some unit tests in Visual Studio, and I want to create a
I know that if port 443 is open that means the remote host supports
I know that when I use range([start], stop[, step]) or slice([start], stop[, step]) ,

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.