我有這樣的代碼,如果一個應用程序第一次啓動。如果它第一次啓動,它將顯示註冊活動,然後在第二次啓動時顯示MainAcitivity。檢查一個按鈕是否在第一次啓動時點擊一個活動android
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
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/phone"
android:layout_centerVertical="true"
android:text="Username"/>
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/username"
android:layout_below="@+id/username"
android:text="Phone No" />
<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/TextView01"
android:layout_centerHorizontal="true"
android:ems="10"/>
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/phone"
android:layout_below="@+id/phone"
android: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();
}
}
有沒有一種方法,我可以放置在檢查登記冊內的活動按鈕被點擊
你需要把 '真' 在你的SharedPreferences在RegisterActivity按鈕點擊 – Vucko
告訴我......... – Blaze
添加按鈕onclickListener,谷歌如何做到這一點,並在'onClick'方法設置您的布爾值爲** false **,因爲它不是第一次運行了。 – Vucko