-1

I have two activities, First One will display user favorite values if set if not set i"ll call second activity set the fav values. When the fav values are set when i run the app next time i should read the data from key value set and display those values in the first activity

Now i can set the key value and read it and show it in the same activity but when i go back or re launch the app the data stored in key value set is not read, can any one help me with what i am missing here, or is there any simple way to achieve this.

5 Answers5

1

for that you must need to implement SharedPreference in your project.. googling for SharedPreferences demo

Prashant Jajal
  • 3,469
  • 5
  • 24
  • 38
1

Set vlaues as:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor edit = pref.edit();
edit.putBoolean("news_icon_flag", true);
edit.putString("key","value");
edit.commit();

Then get these values whereever needed in any activity as follows:

pref.getString("key","defaultvalue");
J0e3gan
  • 8,740
  • 10
  • 53
  • 80
Harry Sharma
  • 2,190
  • 2
  • 15
  • 41
1

for storing data into android see official Doc

in your case you may use SharedPreference

when you are in firstActivity, pass value to your second activty like this

 Intent intent = new Intent(FirstActivty.this, SecondActivity.class);
 intent.putextra("key","value");
 startActivity(intent);

while doing this you are able to pass velue in secondActivity now you are in second activity

Intent intent = getIntent();
String = intent.getStringExtra("key");

 SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("key", value);
 editor.commit();

for retrieving data from SharedPreferences

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("key", null);
MilapTank
  • 9,988
  • 7
  • 38
  • 53
0

You can use intents to pass data between activites.

Intent intent = new Intent(current.this, Destination_activity.class);
intent.putextra("keyName","value");
startActivity(intent);

In your destination activity use this to get the "value" sent

 String data = getIntent().getExtras().getString("keyName");
archon92
  • 447
  • 3
  • 13
0

If you want to read the data from second activity to first activity you have to set data in Intent Bundle and listen it in onActivityForResult().

When you launch your second activity from fists activity do it like:-

Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent,REQUEST_CODE);

And override method in same activity:-

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            String data = data.getStringExtra("fav");;
            SharedPreferences pref = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());

           Editor edit = pref.edit();
           edit.putString("favourities", data);  //edit.putString("key","value");
           edit.commit();
           //More about SharedPref you can find [here][1]
        }
    }

and in your second activity when you set your data do like:-

String favourities=editText1.getText().toString();  
                Intent intent=new Intent();  
                intent.putExtra("fav", favourities);  

                setResult(RESULT_OK,intent);  

                finish();//finishing activity 

More about onActivityForResult you can find here

And when next time you launch your app you can get your data from SharedPref by calling

SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        String fav= pref.getString("favourities", null);
VikasGoyal
  • 3,308
  • 1
  • 22
  • 42