如果第一次没有运行,如何让应用程序转到另一个页面

AI.*_*AI. 12 android button sharedpreferences

我有RegisterPageLoginPage.当应用程序运行时,它将检查应用程序是否是第一次运行RegisterPage.如果它是第一次运行并且button未单击保存,则它将进入RegisterPage.如果它第二次运行但是从不点击保存按钮,它也会保留RegisterPage.否则就会去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
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题是当我单击"保存"按钮并退出应用程序时.当我再次运行应用程序时,它仍然在RegisterPage中,而不是在LoginPage中.

Nai*_*nal 5

在将数据插入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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)