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

  • SEARCH
  • Home
  • 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 8875633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:01:19+00:00 2026-06-14T19:01:19+00:00

package com.example.test3; import java.util.HashMap; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.DialogFragment; import android.app.FragmentManager;

  • 0
package com.example.test3;

import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Test3 extends Activity {

    Button testShowPinDialogButton;
    public AlertDialog alertCreate;
    AlertDialog.Builder alert;
    private HashMap<String, Boolean> pinDialogState;
    EditText input;
    Context context;
    private String tag = "Test3";
    private String click1 = "click1";
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test3);
        context = this;
        sharedPreferences = context.getSharedPreferences(click1, MODE_PRIVATE);
        editor = sharedPreferences.edit();
        testShowPinDialogButton = (Button) findViewById(R.id.testShowPinDialogBbutton);
        testShowPinDialogButton.setOnClickListener(showPinDialog);
        pinDialogState = new HashMap<String, Boolean>();
        Log.d(tag, "onCreate()");
    }

    private OnClickListener showPinDialog = new OnClickListener() {

        @Override
        public void onClick(View v) {
            launchDialog();
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_test3, menu);
        return true;
    }

    protected void launchDialog() {
        alert = new AlertDialog.Builder(context);
        alert.setTitle("Title");
        alert.setMessage("Message");

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

            }
        });

        alert.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                });

        Log.d(tag, "launchDialog()");
        alertCreate = alert.create();
        alertCreate.show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(tag, "onPause()");
        if (alertCreate != null) {
//          alertCreate.dismiss();
            editor.putBoolean(click1, true);
            editor.commit();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.d("Test3", "onResume()");
        boolean isShown = sharedPreferences.getBoolean(click1, false);
        if (isShown) {
//          alertCreate.show();
//          launchDialog();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Test3", "onDestroy()");
    }

}

The alert dialog stays there if I home out on it. So, if I click the show dialog button and then home out and come back to the app, the dialog is still there. But if I turn on the “Don’t keep activities” option under developer options then the alert dialog is not there. Is it possible to keep it there?

  • 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-14T19:01:20+00:00Added an answer on June 14, 2026 at 7:01 pm

    The easiest way (but deprecated now) to do this is to use showDialog(int) and override the onCreateDialog(int) method and provide your custom implementation. This will show your dialog (duh) and re-display it if your activity is recreated (e.g. orientation switch).

    Original (non fragment) dialog tutorial

    Once you get this working you might want to try to use DialogFragment instead, which is the way to do this going forward.

    Documentation on modern Fragment Dialogs

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

Sidebar

Related Questions

package com.example.test3; import java.util.HashMap; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.DialogFragment; import android.app.FragmentManager;
package com.example.helloandroid; import java.util.ArrayList; import java.util.Arrays; import android.app.TabActivity; import android.os.Bundle; import android.view.Window; import android.widget.ArrayAdapter;
I'm trying out the LoaderCursor example in the API Demo: package com.example.android.apis.app; import android.app.Activity;
I have this code: package com.powergroupbd.timer; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer;
package com.example.t2noob; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri;
The code is also at https://github.com/timp21337/java-generics-example package a; import java.util.List; public interface Container<T> {
here is my Code package com.example.messenger; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress;
i try to create small example to test GPS here package org.example.location; import com.google.android.maps.MapActivity;
I am using Java to modify Selenium RC test cases. package com.example.tests; import com.thoughtworks.selenium.*;
Android kills my service in the package com.net.myspeechservice under some certain scenarios, for example

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.