简体   繁体   English

每次返回 MainActivity 时 AlertDialog 都会重新启动

[英]AlertDialog restarted each time I return to MainActivity

I created a MainActivity in which the user has a few app options, displayed in a grid menu, which access subsequent specific activities.我创建了一个 MainActivity,其中用户有几个应用程序选项,显示在网格菜单中,可以访问后续的特定活动。 However, when the application starts, I use an AlertDialog for the user to enter login details, inflated just after the grid layout definition.但是,当应用程序启动时,我使用 AlertDialog 供用户输入登录详细信息,它在网格布局定义之后膨胀。 The problem is, each time I select an item in the grid menu (and, consequently, a new activity), the AlertDialog pops-up again.问题是,每次我 select 网格菜单中的一个项目(以及一个新活动)时,AlertDialog 都会再次弹出。 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.此外,我有一个上传服务,它应该从 MainActivity 的开头开始(或者可能是在登录之后),但不应在每次调用新活动时重新启动。 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.我认为这个问题与前一个问题有关,尽管我已经设法通过 OptionsMenu 使用 startService 按钮暂时解决了它。 This is no permanent solution.这不是长久之计。

Thank you in advance.先感谢您。

EDIT: I tried to use getSharedPreferences as follows:编辑:我尝试按如下方式使用 getSharedPreferences:

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.但是,这种方式 hasLoggedIn 值被保存为 1,对话框再也不会弹出。 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).此外,我相信此操作会影响后续活动(重新设置 alertDialog)。 Which should be a valid alternative to this?哪个应该是一个有效的替代方案?

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 .一种简单的方法是在用户登录后使用SharedPreferences存储一个名为 hasLoggedIn 的hasLoggedIn变量,您将此值设置为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.每次您的主要活动启动时,只需检查hasLoggedIn的值,如果它设置为false ,则需要用户再次登录。 If it is already true don't show the log in dialog如果已经为真,则不显示登录对话框

You can try this: Add a boolean flag in your MainActivity:你可以试试这个:在你的 MainActivity 中添加一个 boolean 标志:

private boolean dialogFlag = true;

in the onCreate/onResume method:在 onCreate/onResume 方法中:

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.并在应用程序启动时先阅读它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM