gpt4 book ai didi

android - 如果应用程序不是第一次运行,如何使应用程序转到另一个页面

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:15 24 4
gpt4 key购买 nike

我有 RegisterPageLoginPage。当应用程序运行时,它会在RegisterPage 中检查应用程序是否是第一次运行。如果是第一次运行并且没有点击保存按钮,它将在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
}
}
}

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

最佳答案

将数据插入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
}
}
}
}

关于android - 如果应用程序不是第一次运行,如何使应用程序转到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41118089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com