Chris.C
Chris.C

Reputation: 297

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? 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.

Upvotes: 1

Views: 1187

Answers (2)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

All you need is startActivityForResult link to demo

Upvotes: 1

Pankaj Jadhav
Pankaj Jadhav

Reputation: 81

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

Upvotes: 2

Related Questions