caps lock
caps lock

Reputation: 511

Need help to save/restore a string between activities

I'm learning Android and I didn't find an answer on the web. I want to save a string from an EditText on my MainActivity to restore it on my third Activity -> Activity3 I want this string to be displayed on a TextView of this Activity.

public class MainActivity extends Activity {

  private EditText nomPrenom;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nomPrenom = (EditText) findViewById(R.id.nomPrenom);
 ...................}

in my activity_main.xml :

android:id="@+id/nomPrenom"
android:text="@string/nomPrenom"

and

public class Activity3 extends ListActivity {

private TextView yourName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity3);
    yourName = (TextView) findViewById(R.id.nomPrenom);
    yourName.setText("take the name of activity 1 here"); //<-- this line causes crash !!


..................}

here is activity3.xml

android:id="@+id/yourName"
android:text="@string/nomPrenom" 

How can I do that ?

Upvotes: 0

Views: 183

Answers (4)

Kameswari
Kameswari

Reputation: 758

In order to move from MainActivity to Activity3 write the following code

//get your edittext's text here
String text = nomPrenom.getText().toString();

//move to Activity3
Intent intent = new Intent(MainActivity.this, Activity3.class);
//pass the edit text value to Activity3
intent.putExtra("EditTextValue", text);
//start Activity3
startIntent(intent);

In Activity3, to get your edit text value do the following.

//get your edit text value here which is passed in MainActivity
String text = getIntent().getStringExtra("EditTextValue");

The variable "text" is the final output which you are looking for.

Upvotes: 0

nilkash
nilkash

Reputation: 7536

You can do it with many ways ...

You can use shared preference so that you can access it from anywhere. But shared preference will retain your value after your application closed.

Another way pass your value with your activity intent when you are creating your activity like this: intent.putExtra' and get value into your activity viagetExtra`

Another way you can define your variable at global place consider create one class too hold such common variables and from there you can access those variable from anywhere.

All three ways can be changed according to your requirement.

Upvotes: 0

Manoj Pal
Manoj Pal

Reputation: 180

First of all your question is not so clear.. But as per given i think you need to fetch a data from edittext ( in main activity ) and then pass it to the third activity .

For This you need to use Intent to pass the data from one activity to other activity.

              Example  - 
              nomPrenom = (EditText) findViewById(R.id.nomPrenom);
              nomPrenom .getText.toString();

             Intent in = new Intent(MainActivity.this, Activity3 .class);
                    in.putExtra("value",nomPrenom .getText.toString()) ;

In Activity3 receive intent like this :-

                       String strValue = getIntent().getExtras().getString("value");
                       yourName = (TextView) findViewById(R.id.yourName);
                       yourName.setText(strValue);

Upvotes: 1

AnujMathur_07
AnujMathur_07

Reputation: 2596

Use Intent.putExtra("Your text here") and pass it to other activities.

or you can take the string from EditText and make the string Global, public and static and then use it in any other class.

Upvotes: 0

Related Questions