gpt4 book ai didi

java - 将 boolean 值传递给上一个 Activity 并在 Android Java 中验证 boolean 值

转载 作者:行者123 更新时间:2023-11-29 19:35:25 32 4
gpt4 key购买 nike

我想讨论一个关于验证 boolean 值以执行 Activity 的不同操作的问题。我应该使用 SharedPreferences 来实现该功能吗?作为一个新手,我想知道哪种方法可以很好地处理我的案例。非常感谢。

上一个 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);
}


}
});
......

单击按钮将转到上一个 Activity :

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

再次为我的愚蠢问题道歉。我正在努力编写更快乐的代码。

最佳答案

Activity 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:

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

关于java - 将 boolean 值传递给上一个 Activity 并在 Android Java 中验证 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39267611/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com