简体   繁体   English

将布尔值传递给Previous活动并在Android Java中验证布尔值

[英]Passing the boolean to Previous activity and verify the boolean in Android Java

I would like to discuss a question about verifying the boolean to act different actions of the activity. 我想讨论一个有关验证布尔值以执行活动的不同操作的问题。 Shall I use SharedPreferences to implement the function? 我应该使用SharedPreferences来实现该功能吗? As a newbie, I would like to know which method will be well performed on my case. 作为一个新手,我想知道哪种方法可以很好地处理我的案件。 Thanks a lot. 非常感谢。

Previous Activity: 上一个活动:

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


            }
});
......

An activity which will go to previous by clicking button: 通过单击按钮将转到上一个活动:

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

Once again, sorry for my stupid question. 再次抱歉我的愚蠢问题。 I am trying hard to code happier. 我正在努力编写更幸福的代码。

您需要的是startActivityForResult 链接以进行演示

Activity A: 活动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");   

                     }  
 } 

Activity B: 活动B:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM