Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将布尔值传递给上一个活动,并在Android Java中验证布尔值_Java_Android_Android Intent_Sharedpreferences - Fatal编程技术网

将布尔值传递给上一个活动,并在Android Java中验证布尔值

将布尔值传递给上一个活动,并在Android Java中验证布尔值,java,android,android-intent,sharedpreferences,Java,Android,Android Intent,Sharedpreferences,我想讨论一个关于验证布尔值以执行活动的不同操作的问题。我应该使用SharedReferences来实现该功能吗?作为一个新手,我想知道哪种方法会在我的案例中得到很好的应用。非常感谢 以前的活动: Boolean isLogged = false; //verify needs to log on or not ............ //include Google Map Fragment btnInsure.setOnClickListener(new View.OnClickListen

我想讨论一个关于验证布尔值以执行活动的不同操作的问题。我应该使用SharedReferences来实现该功能吗?作为一个新手,我想知道哪种方法会在我的案例中得到很好的应用。非常感谢

以前的活动:

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

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

您所需要的只是
开始策略性纠正结果

活动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();

感谢您的建议:)如果选中的用户从第四个活动(用户已登录)返回第一个活动,我该怎么办,在第一个活动中单击一次,将其转到第四个活动?解决方案是Sharedpreferance。用户登录后,您可以在首选项中存储一个布尔登录标志,然后根据布尔标志执行条件操作。感谢您的建议:)如果选中的用户从第四个活动(用户已登录)返回到第一个活动,并通过在第一个活动中单击一次将其返回到第四个活动,我该怎么办?