Cássio Bruzasco
Cássio Bruzasco

Reputation: 183

get EditText data on another activity - android

I have a login field and I want to get this data and put on textView on another Acitivity, but there is one Activity between this two.

Follow what I have so far: MainActivity

Intent i = new Intent(MainActivity.this, FirstPage.class);
                i.putExtra("username", loginField.getText().toString());

A third Activity: TextView textConta = (TextView)findViewById(R.id.textConta);

    textConta.setText("*" + getIntent().getExtras().getString("username"));

and the textView is empty. Any clue? Thanks

Upvotes: 0

Views: 1994

Answers (6)

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

Create a simple DataHolder class

public class DataHolder {
    public static String SOME_STRING;
}

In one activity just set DataHolder.SOME_STRING = editText.getText().toString();

In the other access it

String abcd = DataHolder.SOME_STRING;

Relly simple right?

Upvotes: 0

Sharath
Sharath

Reputation: 691

Once the activity is been out of focus,all the values got by it and the activity itself will be lost.so we have to take the values to the 2nd activity and then to the third activity.It is the only way.We can use intents for it and values can be stored in bundle.

Upvotes: 0

Mike Ma
Mike Ma

Reputation: 2027

also you can use the static variable in a content class as follow:

public class User{
static string username = "xxxx";
}

you can get the username at any activity you want. Because this is only one variable I do not suggest to use SharedPreferences.

another way is using "application"

public class User extends application{
private String username= "xxxx";

public String getUsername() {
    return username;
}
}

and in another activity you can get the application class as follow method:

User user = getApplication();
String username = user.getUsername();

android Clipboard is third way the get the message you want.

Upvotes: 0

Dhinakaran Thennarasu
Dhinakaran Thennarasu

Reputation: 3356

In the activity where you have login field:

SharedPreferences login = getSharedPreferences("LOGIN", 0);
 SharedPreferences.Editor editor = login.edit();
 editor.putString("user", loginField.getText().toString());
 editor.commit();

In the activity where you want to display:

 SharedPreferences login = getSharedPreferences("LOGIN", 0);
 String user = login.getString("user", "0");
textview.setText(user);

Upvotes: 0

mattfred
mattfred

Reputation: 2749

You can use SharedPreferences very easily. Save the string in the first activity:

SharedPreferences settings = getSharedPreferences("AppName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", loginField.getText().toString());
editor.apply();

Then get the string in any activities after that:

SharedPreferences settings = getSharedPreferences("AppName", Context.MODE_PRIVATE);
settings.getString("username", "");

Upvotes: 0

Artem Zinnatullin
Artem Zinnatullin

Reputation: 4447

You need to pass your intent from 2nd Activity to 3rd.

Or as alternative solution, you can store the data that you want to pass between activities in File, SharedPreferences, SQLiteDatabase or other storage.

Upvotes: 1

Related Questions