Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – is there a way for the contents on TextView will not disappear even after relaunching the app?

Posted on November 14, 2022

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.

READ  [ANSWERED] flutter - Android Studio Emulator not starting showing AVD was killed
Powered by Inline Related Posts

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:

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress