2016-09-01 7 views
1

ブール値を検証して、アクティビティのさまざまなアクションを実行する質問について議論したいと思います。 SharedPreferencesを使用して関数を実装しますか?初心者として、私の場合にどの方法がうまくいくかを知りたい。どうもありがとう。ブール値をPreviousアクティビティに渡し、Androidのブール値を確認するJava

前の活動:ボタンをクリックすることで、前に行きます

Boolean isLogged = false; //verify needs to log on or not 
............ 
//include Google Map Fragment 
btnInsure.setOnClickListener(new View.OnClickListener(){ 

      public void onClick (View view){ 
       if(countryCode == null) { 
        Snackbar.make(findViewById(R.id.parentLayout),"Please select a country to continue.", Snackbar.LENGTH_LONG).show(); 
       } else { 
        if(isLogged){ //if true and go to the page which skipped login page 
         Intent intentLogged = new Intent(map_travel.this, travel_trip.class); 
         intentLogged.putExtra("country code", countryCode); 
         Log.e("country code:", String.valueOf(countryCode)); 
         startActivity(intentLogged); 
        } else { //go to login page first 
         Intent intent = new Intent(map_travel.this, travel_login.class); 
         intent.putExtra("country code", countryCode); 
         intent.putExtra("country name", CountryName); 
         Log.e("country code:", String.valueOf(countryCode)); 
         Log.e("country name:", String.valueOf(CountryName)); 
         startActivity(intent); 
        } 


      } 
}); 
...... 

アクティビティ:もう一度

Boolean isLogged = true; //Logged 
...... 
btn_changeCountry.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View view){ 
      currentInsureCountry = null; 
      Intent intentChooseCountry = new Intent(travel_trip.this, map_travel.class); 
      intentChooseCountry.putExtra("logged", isLogged); 
      startActivity(intentChooseCountry); 
     } 
    }); 
...... 

、私の愚かな質問には申し訳ありません。私はもっ​​と幸せにコード化するのを頑張っています。

答えて

1

必要なのはstartActivityForResultlink to demo

+0

感謝:) –

+0

チェックし、ユーザーがバックに入れた場合、私は何ができます4回目のアクティビティの1回目のアクティビティ(ユーザがログに記録されます)を1回目のアクティビティでonClickによって4回目のアクティビティにしますか? –

+0

解決策は共有されています。ユーザーがログインした後、ブール値記録フラグを優先的に保存し、ブール値フラグに基づいて条件付き操作を実行することができます。 https://developer.android.com/training/basics/data-storage/shared-preferences.html –

0

活動Aです:

Intent intent=new Intent(A.this,B.class); 
     startActivityForResult(intent, 2); 

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
      super.onActivityResult(requestCode, resultCode, data); 
       // check if the request code is same as what is passed 
       if(requestCode==2) 
        { 
         boolean flag=data.getStringExtra("MESSAGE"); 

        } 
} 

アクティビティB:あなたの提案のための

boolean flag = true; 
       Intent intent=new Intent(); 
       intent.putExtra("MESSAGE",flag); 
       setResult(2,intent); 
       finish(); 
+0

提案していただきありがとうございます:) –

+0

チェック済みのユーザーが4回目のアクティビティから1回目のアクティビティに戻ると、誰ができますか?第1回目のアクティビティでonClickによって4回目のアクティビティに変わる? –