0

I have the following problem:

I have coded an android app which contains 2 activities. The first Activity is the main activity, the second is for settings.

In the settings Activity you can enter your name with an EditText box, the name should be showed in the first Activity as greeting.

How can I do this in an very simple way?

Nick Udell
  • 2,420
  • 5
  • 44
  • 83
  • [How do I pass data between Activities](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – ThaMe90 Apr 24 '14 at 13:10
  • Have a look at the guide on [developer.android.com](http://developer.android.com/guide/topics/ui/settings.html) – germi Apr 24 '14 at 13:11
  • You can read all about that in this tutorial: http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html#preferences – Boban S. Apr 24 '14 at 13:11
  • If you want a sort of "settings" activity, I think learning about preferences/preferenceactivity is an easy/good way to do it. Considering you are beginning android, I found this to be very useful and engaging to newcomers: [link](https://www.youtube.com/watch?v=zJ9qzvOOjAM). I gave you the link for the "first" video in the playlist for learning preferences. It should only take a few more videos until you learn all about preferences. – Cherry Apr 24 '14 at 13:19
  • there are 2 questions you have asked: 1. how to pass values between activities? 2. how to store and restore values to and from SharedPreferences? Preferences are for longer use, to persist values you can request after restart. To pass values between activities you can use Bundle/arguments. maybe it helps. – drdrej Apr 24 '14 at 13:20

3 Answers3

1

You want to do it with SharedPreferences? This is how you save your data from the settings activity. Add a button to save your preferences once you introduced the name and put this code on click listener:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString("NAME", yourEditText.getText().toString());
editor.commit();

And then in your main activity, add a button and a textview to show the name. Add the following code to the button:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = sharedPreferences.getString("NAME", null);
yourTextView.setText(name); //Add the name to your textview

Hope it helps!!

kodartcha
  • 1,063
  • 12
  • 23
1

You have two ways for this.

First: onActivityResult

In Main Activity:

Use startActivityForResult(intent, ACTIVITY2)

and add this method

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent intent)
{
    if (resultCode == Activity.RESULT_OK)
    {
        String string_1 = intent.getStringExtra ("my_value_from_second_activity", "");
    }
}

In Settings Activity:

Intent intent = new Intent ();
intent.putExtra ("my_value_from_second_activity", "hello");
activity.setResult (Activity.RESULT_OK, intent);

Second: SharedPreferences, the good way

Setting Activity:

SharedPreferences settings = getSharedPreferences("config", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("my_value", edttext.getText().toString());
editor.commit();

Main Ativity:

SharedPreferences settings = getSharedPreferences("config", 0);
String value        = settings.getString("my_value", "");
extmkv
  • 1,991
  • 1
  • 18
  • 36
1

You can pass data through intent from one activity to to another You can also use constant class with static variable to store value and shared prefrenct store data till apk is installer so u can either use of one

Ashish
  • 101
  • 9