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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:29:24+00:00 2026-06-01T10:29:24+00:00

I created a MainActivity in which the user has a few app options, displayed

  • 0

I created a MainActivity in which the user has a few app options, displayed in a grid menu, which access subsequent specific activities. However, when the application starts, I use an AlertDialog for the user to enter login details, inflated just after the grid layout definition.
The problem is, each time I select an item in the grid menu (and, consequently, a new activity), the AlertDialog pops-up again. How can I avoid this?

Moreover, I have an uploading service which should start with the beginning of the MainActivity (or after the login, perhaps), but should not be restarted each time a new activity is called. I assume this problem is related to the previous one, although I have managed to temporarily solve it by using a startService button via an OptionsMenu. This is no permanent solution.

Thank you in advance.

EDIT: I tried to use getSharedPreferences as follows:

private SharedPreferences prefs;
private String prefName = "MyPref";
int hasLoggedIn;

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

    SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
    hasLoggedIn = prefs.getInt("hasLoggedIn", 0);

    if (hasLoggedIn == 0) {
        showDialog(SHOW_DIALOG);
        prefs = getSharedPreferences(prefName , MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("hasLoggedIn", 1);
        editor.commit();
    }

However, this way the hasLoggedIn value is saved as 1 and the dialog never pops-up again. I tried setting the back button to fix that, but this seems to prevent the app from being minimized. Is there a way to add that action to the button? (Which I would duplicate on the Home button as well)

@Override
public void onBackPressed() {
    prefs = getSharedPreferences(prefName , MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt("hasLoggedIn", 0);
    editor.commit();
    Log.i("hasLoggedIn", hasLoggedIn + "");
    return;
}

Moreover, I believe this action will affect subsequent activities (setting the alertDialog back on). Which should be a valid alternative to this?

  • 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-01T10:29:25+00:00Added an answer on June 1, 2026 at 10:29 am

    Basically you need to keep track of your applications states, you have a few options to do this. One simple way would be to use a SharedPreferences to store a boolean variable called something like hasLoggedIn after the user logs in you set this value to true. Each time your main activity launches simply check the value of hasLoggedIn if its is set to false require the user log in again. If it is already true don’t show the log in dialog

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

Sidebar

Related Questions

I'm really new to android and I've created an app that has buttons which
I have a user preference in my app, which gets used by different activities.
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
Here's the problem. In my app, I have 5 tabs which contains activities. In
About App:--This is a simple app which find the user current location. Problem:-- The
Myself designing an app which uses GPS and Maps. I have created the UI
My app has login, once the access is granted, it creates a new html
I created an application which enables the user to set whether he wants to
I am trying to build an App which is swapping its fragments by interactions
I got the following scenario: I am writing an app, which acts like a

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.