AlertDialog restarted each time I return to MainActivity

Go To StackoverFlow.com

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?

2012-04-04 17:22
by Cath
Paste your code... we can't predict how to solve your problem without it - Cristian 2012-04-04 17:26
@Cristian, do you have any suggestions after viewing the code? Thanks - Cath 2012-04-09 15:09


1

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

2012-04-04 17:28
by slayton
That's a great idea, how have I not thought of that?! Thank you, I'll try that - Cath 2012-04-04 18:25
I clearly have not yet mastered sharedPreferences. I will edit my question with the present code - Cath 2012-04-05 01:27
can you help me with the new problem? @slayto - Cath 2012-04-09 15:10
@Cath If you want your app to quit on the onBackPressed method you must tell the app that. You've overridden the default onPressedMethod. To close your Activity when the back button is pressed simply add a call to finish() in your onBackPressed() metho - slayton 2012-04-09 19:47
If I finish this activity, that would kill the service I want to keep running in the background as well, right? I just wanted to change the hasLoggedIn to false to demand the login when the app is opened again. Furthermore, this does not solve the problem I am having with the Activities called by the Main one, because the OnBackPressed defined will set the hasLoggedIn to false each time I return to the MainActivity. @slayto - Cath 2012-04-09 21:27
@Cath, no services are independent from Activities, they have their own life cycle and are killed separately from the activities that spawn the - slayton 2012-04-09 21:58
Oh, great, I did not know that. On the other hand, if I reopen the app, which starts the service, will there be two equal services or android detects one is already running? And what about the onBackPressed if I am not on the MainActivity, will it close the secondary activity? I still am not sure what is the best approach to change the hasLoggedIn only if the app is minimized/closed. @slayto - Cath 2012-04-09 22:05
@Cath, we're really getting off topic here, its probably better you start a new question about service - slayton 2012-04-09 22:44


0

You can try this: Add a boolean flag in your MainActivity:

private boolean dialogFlag = true;

in the onCreate/onResume method:

if(dialogFlag) {
    createDialog();
    dialogFlag = false;
}

If you want to pop up just once the app is installed, you can save this flag into a property file. And read it first whenever the app is getting started.

2012-04-04 17:38
by Javatar
This won't work if the MainActivity object is destroyed before the activity resumed, as a new object will be generated and the flag will be the default value of true - slayton 2012-04-04 18:26
Yes, if that happens you should may create a manager class which holds states alive - Javatar 2012-04-05 08:28
Why do that when the SharedPrefernces already exist to do just this - slayton 2012-04-05 12:49
Ads