java检查首次启动安卓时是否在活动上单击了按钮
我有一个代码,如果一个应用第一次启动。如果第一次启动,则显示注册活动,然后在第二次启动时显示MainActivity
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isFirstRun", false);
if (isFirstRun) {
startActivity(new Intent(Register.this, MainActivity.class));
//Toast.makeText(Register.this, "Authenticated", Toast.LENGTH_LONG) .show();
//finish the application after first run
finish();
}
{
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("isFirstRun", true).commit();
// finish();
}
上面的代码工作正常,但如果用户没有单击“注册”活动中的按钮并再次启动应用程序,它会将用户带到MainActivity,因此用户没有注册,而是使用了该应用程序。如何检查注册活动中的按钮是否在首次启动时被单击,如果没有,如何将用户导航回注册活动
用于用户注册的xml
<TextView
安卓:id="@+id/textView1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignLeft="@+id/phone"
安卓:layout_centerVertical="true"
安卓:text="Username"/>
<EditText
安卓:id="@+id/username"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignLeft="@+id/textView1"
安卓:layout_below="@+id/textView1"
安卓:ems="10" />
<TextView
安卓:id="@+id/TextView01"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignLeft="@+id/username"
安卓:layout_below="@+id/username"
安卓:text="Phone No" />
<EditText
安卓:id="@+id/phone"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/TextView01"
安卓:layout_centerHorizontal="true"
安卓:ems="10"/>
<Button
安卓:id="@+id/register"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignRight="@+id/phone"
安卓:layout_below="@+id/phone"
安卓:text="Register" />
更新代码
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.register) {
// if(!validate()){
// call AsynTask to perform network operation on separate thread
new HttpAsyncTask().execute("http://xyz/create");
//new AttemptRegistration().execute();
// }
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isFirstRun", false);
if (isFirstRun) {
//show start activity
startActivity(new Intent(Register.this, MainActivity.class));
//Toast.makeText(Register.this, "Authenticated", Toast.LENGTH_LONG) .show();
//finish the application after first run
finish();
}
{
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("isFirstRun", true).commit();
// finish();
}
}
有没有办法检查注册活动中的按钮是否被点击
# 1 楼答案
当然。当应用程序打开时,不要保存
isFirstRun
布尔值,而是在单击注册按钮时保存它。只需将逻辑移到注册按钮的onClick
方法中。但是,您仍然需要检查Register
活动的onCreate
方法中的布尔值。像这样:然后,在
onClick
中: