2016-05-13 7 views
1

アプリが最初に起動された場合、このコードがあります。初めて起動するとRegisterアクティビティが表示され、2回目の起動時にMainAcitivityが表示されます。最初の起動時にアクティビティのボタンがクリックされていないか確認してください。

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にユーザーをとり、これにより、ユーザは登録されませんでしたが、アプリを利用します。最初の起動時に登録アクティビティのボタンがクリックされたことを確認する方法と、ユーザーを登録アクティビティに戻す方法を確認する方法はありますか。ユーザ登録

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

     } 

ため

XMLレジスタ活性のボタンがクリックされたことを、私はチェックを置くことができる方法はあります

+0

あなたはボタンのクリック – Vucko

+0

にRegisterActivityであなたのSharedPreferencesに '真' に置く必要がある私を見る......... – Blaze

+0

このようなボタンonclickListener、googleを追加する方法を 'onClick'メソッドで設定します。これは最初の実行ではないので、** false **にブール値を設定します。 – Vucko

答えて

0

確かに。アプリが開くときにisFirstRunブール値を保存する代わりに、登録ボタンがクリックされたときに保存します。ロジックを登録ボタンのonClickメソッドに移動するだけです。ただし、RegisterアクティビティのonCreateメソッドでブール値をチェックする必要があります。あなたのonClickで次に

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

} 

、::

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

上記のように私が起動するたびに、登録アクティビティ。しかし、ユーザーがこのアクティビティの登録ボタンをクリックしたときに一度だけ表示されるようにします。私の質問をコード – Blaze

+0

で更新しますか?コードは役に立ちます – mattfred

+0

更新されたコードを確認してください – Blaze

関連する問題