I want to get data from 2 activities( Activity and fragmentActivity) and I want to use this data in another class. Is there any way to share data among activities without using intent?
Asked
Active
Viewed 76 times
-2
-
2There are many ways like : Shared Preferences,Database and Static. Choose which more convenient for you. – Haresh Chhelana Jun 08 '15 at 13:19
-
Yes. There are so many options as suggested in above comment !! – Piyush Jun 08 '15 at 13:20
2 Answers
1
There is multiple ways to achieve that :
Method 1:
Use static class setter and getter method:
create static class and set values from first activity and get value from second activity
Method 2:
Post your values through the intent
Method 3:
Use database to store data from one activity and get data from other activity
Method 4:
Use Shared preference

Amsheer
- 7,046
- 8
- 47
- 81
0
Use SharedPreferences
like this:
//in the first activity where you want to save some values
SharedPreferences prefs = this.getSharedPreferences("com.myPackage.myApp", Context.MODE_PRIVATE);
prefs.edit().putString("name", "value").apply(); //the first parameter is the name with which you will fetch the value later, the cond one is the actual value
....
//fetch your data from another activity:
SharedPreferences prefs = this.getSharedPreferences("com.myPackage.myApp", Context.MODE_PRIVATE);
String myVariable = prefs.getString("name", ""); //second parameter is a default value; you could use all primitive variable types like getString, getBoolean, getLong etc
P.S. if you use the Sharedpreferences
in a Fragment, change the this
with getActivity()
and everything else is the same, just because the this
doesn't mean the same in a Fragment as in an Activity

Gabriella Angelova
- 2,985
- 1
- 17
- 30