Android 如果应用程序不是第一次运行,如何让它转到另一个页面

Android 如果应用程序不是第一次运行,如何让它转到另一个页面,android,button,sharedpreferences,Android,Button,Sharedpreferences,我有注册页面和登录页面。当应用程序运行时,它将在注册表页中检查应用程序是否首次运行。如果它是第一次运行并且未单击保存按钮,它将在注册表页中显示。如果第二次运行,但从未单击“保存”按钮,它也将保留在注册表中。否则它将转到LoginPage 这里是我的更新代码 注册 appGetFirstTimeRun(); boolean clicked=false; buttonSave.setOnClickListener(new View.OnClickListener() {

我有
注册页面
登录页面
。当应用程序运行时,它将在
注册表页
中检查应用程序是否首次运行。如果它是第一次运行并且未单击保存
按钮
,它将在
注册表页
中显示。如果第二次运行,但从未单击“保存”按钮,它也将保留在
注册表中。否则它将转到
LoginPage

这里是我的更新代码

注册

appGetFirstTimeRun();
boolean clicked=false;

 buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clicked=true;
                int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
                SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
                appPreferences.edit().putInt("app_second_time",
                        appCurrentBuildVersion).apply();
                String name = editTextName.getText().toString();
                String pass = editTextPassword.getText().toString();
                String confirm = editTextConfirm.getText().toString();
                if ((editTextName.getText().toString().trim().length() == 0) || (editTextPassword.getText().toString().trim().length() == 0) || (editTextConfirm.getText().toString().trim().length() == 0)) {
                    Toast.makeText(getApplicationContext(), "Field cannot be null", Toast.LENGTH_LONG).show();
                } 
                else
                 {
                      insertData(name, pass, imageUri); // insert to SQLite
                      Intent intent = new Intent(MainActivity.this, AddMonthlyExpenses.class);
                      intent.putExtra("name", name);
                      startActivity(intent);
                  }
            }
        });

  private int appGetFirstTimeRun() {

        //Check if App Start First Time
        SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
        int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
        int appLastBuildVersion = appPreferences.getInt("app_first_time", 0);

        if (appLastBuildVersion == appCurrentBuildVersion && clicked) {
            Intent intent = new Intent(MainActivity.this,LoginPage.class);
            startActivity(intent);
            return 1;
        } else {
            appPreferences.edit().putInt("app_first_time",
                    appCurrentBuildVersion).apply();
            if (appLastBuildVersion == 0) {
                Toast.makeText(getApplicationContext(), "First time", Toast.LENGTH_SHORT).show();
                return 0; //es la primera vez
            } else {
                return 2; //es una versión nueva
            }
        }
    }

问题是当我点击save按钮并退出应用程序时。当我再次运行应用程序时,它仍在注册表页中,而不是登录页中。

您还需要在单击“保存”按钮时更改共享首选项值。只有下次打开app appGetFirstTimeRun方法时,才会加载登录页面

在您的btnSave中,单击正在开始活动意图的侦听器,然后在startActivity中添加此代码

int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
    SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);

   appPreferences.edit().putInt("app_first_time",
                appCurrentBuildVersion).apply();

将此代码添加到初始屏幕

 SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(context);
    boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", false);
    if (!isFirstRun) {
startActivity(new Intent(context,RegirstorActivity.class));
}else{
startActivity(new Intent(context,LoginActivty.class));
}

所以基本上它会决定是第一次(用户注册)还是没有,注册后更新SharedReferences

您正在使用click变量来确定按钮是否被单击,但当您从应用程序中存在时,再次单击值重置为false,这样您就可以使用共享首选项来保存按钮单击时的值true或false并从共享中获取值,而不是将值保存在click变量中优先检查

单击“保存”按钮

   appPreferences.edit().putInt("app_second_time",
                    appCurrentBuildVersion).apply();   

我想这可能就是答案。在您的代码上,
clicked boolean
变量未存储在共享首选项中,但您正在检查appGetFirstTimeRun()中的条件(第二次启动时可能未单击按钮,但条件必须为true),因此通过 添加此行
appPreferences.edit().putBoolean(“first_run”,true).apply()打开
按钮保存
单击侦听器,然后添加此行
clicked=appPreferences.getBoolean(“首次运行”,false)
appGetFirstTimeRun()函数上
完整的代码是

appGetFirstTimeRun();
boolean clicked=false;

 buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
                SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
                appPreferences = appPreferences.edit().putBoolean("first_run",
                true).apply(); //***Add this ****
                appPreferences.edit().putInt("app_second_time",
                        appCurrentBuildVersion).apply();
                String name = editTextName.getText().toString();
                String pass = editTextPassword.getText().toString();
                String confirm = editTextConfirm.getText().toString();
                if ((editTextName.getText().toString().trim().length() == 0) || (editTextPassword.getText().toString().trim().length() == 0) || (editTextConfirm.getText().toString().trim().length() == 0)) {
                    Toast.makeText(getApplicationContext(), "Field cannot be null", Toast.LENGTH_LONG).show();
                } 
                else
                 {
                      insertData(name, pass, imageUri); // insert to SQLite
                      Intent intent = new Intent(MainActivity.this, AddMonthlyExpenses.class);
                      intent.putExtra("name", name);
                      startActivity(intent);
                  }
            }
        });

  private int appGetFirstTimeRun() {

        //Check if App Start First Time
        SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
        int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
        int appLastBuildVersion = appPreferences.getInt("app_first_time", 0);
        clicked = appPreferences.getBoolean("first_run", false); //*** Add this ***
        if (appLastBuildVersion == appCurrentBuildVersion && clicked) {
            Intent intent = new Intent(MainActivity.this,LoginPage.class);
            startActivity(intent);
            return 1;
        } else {
            appPreferences.edit().putInt("app_first_time",
                    appCurrentBuildVersion).apply();
            if (appLastBuildVersion == 0) {
                Toast.makeText(getApplicationContext(), "First time", Toast.LENGTH_SHORT).show();
                return 0; //es la primera vez
            } else {
                return 2; //es una versión nueva
            }
        }
    }

您依赖于
SharedReferences
以及
clicked
变量,您可以依赖
SharePreferences
但不依赖于变量,因为每次运行时您都将clicked值设置为false

1) 单击按钮时,在首选项中保存当前版本

appPreferences.edit().putInt("app_first_time",
                appCurrentBuildVersion).apply();
appPreferences.edit().putBoolean("clicked",
                true).apply();
2) 单击按钮时在首选项中保存单击的值

appPreferences.edit().putInt("app_first_time",
                appCurrentBuildVersion).apply();
appPreferences.edit().putBoolean("clicked",
                true).apply();
现在在您的
appGetFirstTimeRun()中
获取版本值,并从
SharedReferences

int appLastBuildVersion = appPreferences.getInt("app_first_time", 0);
boolean clicked = appPreferences.getBoolean("clicked", false);

在将数据插入SQLite后单击复选按钮,这样您就可以确认您的数据已成功保存,并可以进入下一个屏幕

在下面的代码中找到我的注释并编辑您的代码:-

public class Register extends AppCompatActivity {
    Button buttonSave;
    boolean clicked=false;//remove this
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        appGetFirstTimeRun();//call this method here
        buttonSave=(Button)findViewById(R.id.button);
        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clicked=true;//remove this
                int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
                SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
                appPreferences.edit().putInt("app_second_time", appCurrentBuildVersion).apply();

                String name = editTextName.getText().toString();
                String pass = editTextPassword.getText().toString();
                String confirm = editTextConfirm.getText().toString();
                if ((editTextName.getText().toString().trim().length() == 0) || (editTextPassword.getText().toString().trim().length() == 0) || (editTextConfirm.getText().toString().trim().length() == 0)) {
                    Toast.makeText(getApplicationContext(), "Field cannot be null", Toast.LENGTH_LONG).show();
                }
                else
                {
                    insertData(name, pass, imageUri); // insert to SQLite
                    appPreferences.edit().putBoolean("btn_clicked", true).apply();//add this line
                    Intent intent = new Intent(Register.this, AddMonthlyExpenses.class);
                    intent.putExtra("name", name);
                    startActivity(intent);
                }
            }
        });
    }
    private int appGetFirstTimeRun() {

        //Check if App Start First Time
        SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
        int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
        int appLastBuildVersion = appPreferences.getInt("app_first_time", 0);
        boolean is_btn_click=appPreferences.getBoolean("btn_clicked",false);//add this line

        if ((appLastBuildVersion == appCurrentBuildVersion) && is_btn_click) { //edit this line like this
            Intent intent = new Intent(Register.this,LoginPage.class);
            startActivity(intent);
            return 1;
        } else {
            appPreferences.edit().putInt("app_first_time",
                    appCurrentBuildVersion).apply();
            if (appLastBuildVersion == 0) {
                Toast.makeText(getApplicationContext(), "First time", Toast.LENGTH_SHORT).show();
                return 0; //es la primera vez
            } else {
                return 2; //es una versión nueva
            }
        }
    }
}

尝试使用以下自定义getter和setter方法

private static SharedPreferences getInstance(Context context) {
    context.getSharedPreferences("MY_APP", Context.MODE_PRIVATE);
    return sharedPreferences;
}

public boolean getAppStatus(Context context) {
    return getInstance(context).getString("FIRST_TIME", false);
}

public void setAppStatus(Context context, boolean status) {
    getInstance(context).edit().putString("FIRST_TIME", status).commit();
}
现在,当您第一次调用getAppStatus()时,或者当用户甚至一次都没有单击“保存”按钮时。它将返回false。当用户单击save按钮时,您可以将“FIRST_TIME”变量的值更新为true。从而验证用户是否与注册页面进行了交互

@Override
public void onClick(View v) {
    if(v.getId()==R.id.save_button)
     setAppStatus(context,true);
}
检查“rstste”是否为false显示注册页面,否则显示登录页面。

onCreate()的start方法,调用
checkStages()

按钮save.onClick()
方法的end中,调用
Prefs.putStage(这个,1)后跟
checkStages()


这是我用来编写任何Android应用程序的示例


您可以从此项目获取Prefs

当您的appGetFirstTimeRun()时,clicked变量始终为false;是called@TabishHussain正确的方法是什么?不要在该函数中使用clicked,而是通过查询数据来检查数据是否插入到数据库中。。因此,如果存在数据,则将其移动到登录页面如何更改共享首选项值?你知道吗?但我不想在我的应用程序中使用启动屏幕。方法是在注册屏幕中直接签入一次创建如果用户已注册,则转到登录屏幕,否则留在注册屏幕上问题是如果我从未单击保存按钮注册并再次启动应用程序,它将进入登录。我希望它确保保存按钮被点击,然后只进入登录