Save user input in edittext

Asked

Viewed 1,997 times

0

I’m new using Android Studio and would like to save the user entry in a edittext And even if the user presses the back button or closes the application the typed text remains on edittext

I wonder if someone could help me ?

  • If you have any code edit the question and add it, tag (blue comic) the language. Get familiar with the site on tour and see some tips on how to ask questions

  • 2

    I suggest taking a look at the API of SharedPreferences. These two links: http://developer.android.com/training/basics/data-storage/shared-preferences.html and http://developer.android.com/reference/android/content/SharedPreferences.html and in addition to these questions: http://answall.com/questions/25167/salvar---string-sharedpreference e http://answall.com/questions/33677/comorsalvar-lista de-objetos-em-android/33698#33698 can help you.

  • @Wakim, I believe you could create an answer =)

  • @Matthew, I prefer not to create an answer because we already have several questions/answers about it, although the question does not refer explicitly, I would characterize as a duplicate...

  • @Wakim, you know, but I think you could use the content of the existing answers and add how to use in the events onCreate and onDestroy, that I believe is the doubt of the AP.

2 answers

1

Android, in turn, provides various forms of data persistence. Among them are:

  1. Save data to the web;

  2. a private database;

  3. External rescue and Intern;

  4. Sharedpreferences.

    In your case, we can use the simplest, which is the Sharedpreferences. This method saves your data(values) in native system keys so that you can access it at any time you wish. Her mode of use is also very simple.



    public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       // Put code

       // Regravar dados, se caso for necessário.
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       String eValue = settings.getString("EditText_value", "xxxx");
    }

    @Override
    protected void onStop(){
       super.onStop();

      // Este método faz com que, quando a aplicação for pausada, seja inserido um valor na Preference. Eu acredito que seja isto que você quer.
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putString("Edittext_value", myEditText.getText().toString()); // Isto é oque você quer.

      // Salvar valor!
      editor.commit();
    }

Observing: To save the value of your Edittext, first take its value and pass it to Sharedpreferences as a string.

For more information, see the Android documentation: Storage Options

0

You will need to control the information of this Edittext using the life cycle of your Activity on Android.

The ideal would be to persist the information of this Edittext, in the methods onPause(), onStop(), and onDestroy() within your Activity. And then return the value of it in the methods onCreate(), onStart(), onRestart(), and onResume().

Reading a little bit and exploring the life cycle of an Activity can help you a lot.

has a very good pdf here

  • Thank you Jhonattas! I will read and try here.

  • Any new question I will be available here, because I was a beginner on Android.

  • Hi jhonnattas tried here but I can not brother at all are several attempts and nothing, I just needed to type something in edittex , close the application and when opening the application find the text typed in edittext. I am new to android and java and without an example of a difficult code. If you can post an example of code I will be grateful

Browser other questions tagged

You are not signed in. Login or sign up in order to post.