Reputation: 615
I have to just transfer save file path from one activity to another activity but when it comes to show the data it display null value.
Following is the code to store data in SharedPreference:-
SharedPreferences pref= getApplicationContext().getSharedPreferences("Recorder",0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("file_name",DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime())+".mp4");
editor.putString("file_path",Environment.getExternalStorageDirectory()+"/"+DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime())+".mp4");
Retrieving data from sharedPreference:-
file_name = (TextView) findViewById(R.id.file_name);
file_path = (TextView) findViewById(R.id.file_path);
pref = getApplicationContext().getSharedPreferences("Recorder",0);
editor = pref.edit();
String fileName = pref.getString("file_name",null);
String filePath = pref.getString("file_path",null);
if(fileName != null){
file_name.setText(fileName);
}else{
file_name.append("");
}
if (filePath != null){
file_path.setText(filePath);
}else{
file_path.append("");
}
Upvotes: 0
Views: 504
Reputation: 3838
Yes you can make use of SharedPrefernces. try below code
public static final String MYPREFERNCES_Example = "mysharedpref";
SharedPreferences.Editor editor ;
SharedPreferences sp;
sp = getSharedPreferences(MYPREFERNCES_Example Context.MODE_PRIVATE);
editor=sp.edit();
Intent i_login = new Intent(LoginActivity.this, HomeActivity.class);
editor.putString("yourownkey1", yourvalue);
editor.putString("yourownkey2", yourvalue);
editor.putString("yourownkey3", yourvalue);
startActivity(intent)
and in next activity
sp = getSharedPreferences(MYPREFERNCES_Example, 0);
editor = sp.edit();
String fn = sp.getString("yourownkey1", "");
String ln = sp.getString("yourownkey2", "");
String fbemail = sp.getString("yourownkey3", "");
Upvotes: 0
Reputation: 201
editor.commit() is missing. Better way is to use bundle that could be set in intent and use this intent to start another activity and then fetch values from intent's bundle.
Upvotes: 0
Reputation: 1
If you are using SharedPreffernces just to share data between activities, then it's overkill.
In case data should be available after app restart your solutions is good. But if you need it only at runtime, it's better to use Intent as mentioned in other answers.
Personally, I would prefer to store it in some static variable.
Upvotes: 0
Reputation: 640
Yeah you can make use of SharedPreference to Store the information.
To pass the data from one activity to another its better to use intent.
Intent intent = new Intent(CurrentActivity.this, AnotherActivity.class);
intent.putExtra("file_name","name");
intent.putExtra("file_path","path");
startActivity(intent)
In another Activity,
String fileName = getIntent().getStringExtra("file_name");
String filePath = getIntent().getStringExtra("file_path");
Upvotes: 0
Reputation: 4230
Why do you need to overload the system ? you have a mechanism for passing data between activities, just send the path of the file within the intent.
In ActivityA :
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("filePath", "...");
startActivity(intent);
In ActivityB :
String filePath = getIntent().getExtras().getString("filePath");
Upvotes: 1
Reputation:
You have to apply the editor by
editor.commit():
or
editor.apply();
Upvotes: 2