SharedPreferences Not Loading Across Activities

  • Replies:0
Vorcax
  • Forum posts: 1

Apr 23, 2014, 7:54:49 PM via Website

I have created a questionnaire in the beginning of the app in order to base a user model off the answers chosen. The values of the chosen options are stored as Shared Preferences as shown below. With the data retrieved I am then trying to tag a Google map with certain tags based on the answers chosen. The issue I am encountering is that of reading the stored Shared Preference values across the different activities, from the Question2 Activity, to that of the Map Activity (code below). Any pointers on how to solve the context conflict would be greatly appreciated.

Question2 Activity:

 public class Question2 extends Activity{

RadioButton q2a1,q2a2,q2a3;
Button btn2;

public static final String MY_PREF = "MyPreferences";

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.activity_question2, null);
    return v;
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question2);

    q2a1 = (RadioButton) findViewById(R.id.q2a1);
    q2a2 = (RadioButton) findViewById(R.id.q2a2);
    q2a3 = (RadioButton) findViewById(R.id.q2a3);
    btn2 = (Button) findViewById(R.id.q2_button);



    btn2.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            SharedPreferences prefernces = getSharedPreferences(MY_PREF, 0);
            SharedPreferences.Editor editor = prefernces.edit();

            if (q2a1.isChecked()){
                editor.putInt("answer_value2", 1);
                editor.commit();
            } 
            if (q2a2.isChecked()){
                editor.putInt("answer_value2", 2);
                editor.commit();
            }
            if (q2a3.isChecked()){
                editor.putInt("answer_value2", 3);
                editor.commit();
            }else {
                editor.putInt("answer_value2", 0);
                editor.commit();
            }
            editor.commit();

            Intent intent = new Intent(getApplicationContext(), Question3.class);
            startActivity(intent);
        }
    });
  }
}

Map Activity:

 public class MapActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapLayout)).getMap();
    map.setMyLocationEnabled(true);

    SharedPreferences preferences = getSharedPreferences(Question2.MY_PREF,MODE_PRIVATE);

    int q1answer = preferences.getInt("answer_value", 0);
    int q2answer = preferences.getInt("answer_value2", 0);
    int q3answer = preferences.getInt("answer_value3", 0);

    if(q1answer == 1){
        //method
    }
    if(q1answer == 2){
        //method
    }
    if(q1answer == 3){
        //method
    }

    if(q2answer == 1){
        map.addMarker(new MarkerOptions().position(FOOD_FOOD).title("Food & Food").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(HEALTHSHOP).title("Health Shop").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(GRASSYHOPPER).title("Grasy Hopper").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(M_S).title("M&S").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(MINT).title("Mint").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

    }if(q2answer == 2){
         Toast.makeText(getApplicationContext(), "Your result is " + q2answer,  Toast.LENGTH_SHORT).show();
        map.addMarker(new MarkerOptions().position(FOOD_FOOD).title("Food & Food").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(SUBWAY).title("Subway").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(NEWYORKSBEST).title("New York's Best").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(MCD).title("Mc Donald's").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(M_S).title("M&S").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(JUBILEE).title("Jubilee").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(GRASSYHOPPER).title("Grassy Hopper").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

    }if(q2answer == 3){
        map.addMarker(new MarkerOptions().position(NEWYORKSBEST).title("New York's Best").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(SUBWAY).title("Subway").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(PASTIZZERIA).title("Pastizerria").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(MCD).title("Mc Donald's").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(BURGERKING).title("Burger King").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    }

    if(q3answer == 1){
        //implementation
    }if(q3answer == 2){
        //implementation
    }if(q3answer == 3){
        //implementation
    }if(q3answer == 4){
        //implementation
    }   

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.map, menu);
    return true;
}

Reply