Solution 1 :
Welcome to StackOverflow !
If you have saved data in Shared Preferences then you simply need use setText()
on text view.
public class MainActivity extends AppCompatActivity {
//your global variables here
@Override
protected void onCreate(Bundle savedInstanceState) {
TextView textView = findViewById(R.id.text_view);
//some more textView or other views initialization here
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
//get saved value from SharedPreferences
String saveText = sharedPreferences.getString("PREFERENCE_KEY", defaultValue);
//some more saved value in similar manner
//set saved value on TextView
textView.setText(saveText);
}
}
As onCreate()
method will execute on every launch of app so as textView.setText(saveText)
.
Now for the very first install of app,there is no data will be saved in SharedPreferences so sharedPreferences.getString()
will use defaultValue you have provied.
If you just want to show some sample data on TextView on each startup, then you can android:text="sample_data_value"
in your layout xml file as follow-
<TextView
.......
android_text="sample_data_value"/>
Happy Coding !
Problem :
I have this app i’m experimenting on. wherein the user will input data, saves it onto sharedpreferences and displays it. I already manage to do those three. except when relaunching the app. the details that should still be displayed on Textview disappears. is there a way for it not to disappear after relaunching?
saved and displayed before relaunching the app
after relaunching the app
Ps. I’m a beginner on android studio
Comments
Comment posted by Nikhil Sharma
do you want to show sample data on text view or user’s saved data on relaunch ?
Comment posted by vikas kumar
try to always add your code so that others can access where it’s getting wrong then guessing what you might have done.
Comment posted by Kyle Vince
I’ve already manage to do that part and display it. I want to display the saved data every startup. do i need to create an argument for that to reoccur every startup?
Comment posted by Nikhil Sharma
no you simply need to use information saved in the Shared Preference and use
Comment posted by Kyle Vince
thanks a lot. it got me a bit confused for a moment. but it worked out.
Comment posted by How does accepting an answer work
Glad to help, if this answer solved your problem please mark it as accepted by clicking the check mark next to the answer. see: