0

My problem is that I need to save the values from a EditText from an Activity in a Java .class file to get that value if I reopen the same Activity. I already searched around some forums and all I could find is about Intent I am still learning Android..

EDIT

I was looking for something like this: This class have the variables;

public class SharedActivity {

public static String convite;

}

In activity 1 I do this:

SharedActivity.convite=EditText value;

In activity 2 I get the value from SharedActivity.convite variable to an EditText, but the value that I get is null, any ideas? Thanks!

Ko0kiE
  • 33
  • 6

2 Answers2

2

Maybe SharedPreferences is what you are looking for

Put this snippet inside any Event handler method e.g. Button's OnClickListener:

//creating an instance of a shared preference with code 'edit_text_code' and only reachable by this application
SharedPreferences mySharedPrefs=getSharedPreferences("my_shared_prefs_file_name",MODE_PRIVATE);
//getting the mySharedPrefs's editor for further editing
SharedPreferences.Editor editor=settings.edit();
//putting the EditText content as a shared preference to be 'commited'
editor.putString("edit_text_code",myEditText.getText().toString());
editor.commit();

And now you should read the "shared preference" and its preferences inside, in this case, your EditText content:

e.g.

public class MainActivity2 extends AppCompatActivity {
TextView myTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        myTextView = (TextView) findViewById(R.id.my_text_view);
        SharedPreferences mySharedPrefs = getSharedPreferences("my_shared_prefs_file_name",MODE_PRIVATE);
        String content = mySharedPrefs.getString("edit_text_code","Show this text in case that shared preference doesn't exist");
        textView.setText("The EditText content was: "+  content);
    }

For further learning on more topics. Check out this blog: http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html

Try this ChalkSreet app about Android Development also: https://play.google.com/store/apps/details?id=com.chalkstreet.learnandroid&hl=en There you can find interesting examples including one very similar to your question

App screen capture 1

App screen capture 2

EDIT

Here is the source of the example I was talking about and it Should work.

public class SharedActivity extends AppCompatActivity {
    EditText et;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_shared);
        et = (EditText) findViewById(R.id.edit_text_shared);
        SharedPreferences settings = getSharedPreferences("PREFS",MODE_PRIVATE);
        et.setText(settings.getString("option",""));
    }

    //This does the trick    
    @Override
    protected void onStop() {
        super.onStop();
        SharedPreferences settings = getSharedPreferences("PREFS",0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("option",et.getText().toString());
        editor.commit();
    }
}

OR You can create a file using Java OutputStream for writing and InputStream classes to store that "variable"

1w3j
  • 566
  • 8
  • 24
  • Hi!,thanks for your reply, but is somethinh more like this that i am looking for: http://stackoverflow.com/questions/14829093/pass-variable-between-non-activity-class-to-android-activity-class, unfortunately when i change activity the value on the variable of that class dissapears, can you help me with that? – Ko0kiE Nov 02 '16 at 22:21
  • There is an example in the app about your question anyway i'm editing the answer – 1w3j Nov 03 '16 at 14:41
  • Do you think using file stream classes would be the solution? – 1w3j Nov 03 '16 at 14:57
  • Hi!!, could you please check my post again? See the edit part, Thanks! – Ko0kiE Nov 03 '16 at 16:01
  • I think you are confused about how `static` variables work. I suggest you further reading on this. Anyways, you should store those variables somewhere out from the instance of your application, because otherwise simply will not work. I proposed you two workarounds. – 1w3j Nov 04 '16 at 03:59
0

You can put your data into SharedPreferences when the Activity stops (override the onStop method) like this

SharedPreferences data = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = data.edit();
      editor.putString("edit_text_data", editText.getText().toString());
      editor.commit();

And when you restore (start) your Activity, you can retrieve the data like this:

SharedPreferences data = getSharedPreferences(PREFS_NAME, 0);
       String editData = data.getString("edit_text_data", "none"); 
//The second parameter is default value if it doesnt find that tag.
woodenleg
  • 84
  • 1
  • 9
  • so if i do this in two activitys, they will be stored in the same SharedPreferences? Or this will work for each activity itself? – Ko0kiE Nov 02 '16 at 17:44
  • For each "data" you want to store, you use a different tag. (not just "edit_text_data") – woodenleg Nov 02 '16 at 22:38
  • The "SharedPreferences" are for your whole app, but you can store there different types of variables. Each item stored has its own tag. – woodenleg Nov 02 '16 at 22:40