2016-05-13 48 views
1

我有這樣的代碼,如果一個應用程序第一次啓動。如果它第一次啓動,它將顯示註冊活動,然後在第二次啓動時顯示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(); 
      } 

     } 

有沒有一種方法,我可以放置在檢查登記冊內的活動按鈕被點擊

+0

你需要把 '真' 在你的SharedPreferences在RegisterActivity按鈕點擊 – Vucko

+0

告訴我......... – Blaze

+0

添加按鈕onclickListener,谷歌如何做到這一點,並在'onClick'方法設置您的布爾值爲** false **,因爲它不是第一次運行了。 – Vucko

回答

0

當然。在應用程序打開時,不要保存您的isFirstRun布爾值,而應在點擊註冊按鈕時保存它。只需將邏輯移入註冊按鈕的onClick方法即可。但是,您仍然需要檢查Register活動的onCreate方法中的布爾值。就像這樣:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // .... 

    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(); 
    } 
    // ..... 

} 

然後,在你onClick

@Override 
public void onClick(View v) { 
    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("isFirstRun", true).commit(); 
// ... 
} 
+0

隨着上述每次啓動它,它都會顯示註冊活動。但是,當用戶點擊此活動的註冊按鈕時,我只希望它顯示一次。我是否使用代碼更新我的問題 – Blaze

+0

代碼會有幫助 – mattfred

+0

請檢查以查看更新的代碼 – Blaze

相關問題