Pass a String from One Activity to Another Activity in Android

How to send string from one activity to another?

You can use intents, which are messages sent between activities. In a intent you can put all sort of data, String, int, etc.

In your case, in activity2, before going to activity1, you will store a String message this way :

Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);

In activity1, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :

Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");

Then you can set the text in the TextView:

TextView txtView = (TextView) findViewById(R.id.your_resource_textview);    
txtView.setText(message);

Hope this helps !

How to pass two Strings from one Activity to another Activity in Android

this way you can pass and retrive string from one activity to another activity

Intent intent = new Intent(AddLocationActivity.this, MapsActivity.class);
intent.putExtra("address", addressString);
intent.putExtra("string2", string2);
startActivity(intent);

retrive String to MapsActivity

Intent intent = getIntent();
String address = intent.getStringExtra("address");
String string2 intent.getStringExtra("string2");

Hope it will help you :)

How do you pass a string from one activity to another?

Pass values using intents.

In your first activity

 Intent i= new Intent("com.example.secondActivity");
i.putExtra("key",mystring);
// for explicit intents
// Intent i= new Intent(ActivityName.this,SecondActivity.class);
// parameter 1 is the key
// parameter 2 is the value
// your value
startActivity(i);

In your second activity retrieve it.

Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//get the value based on the key
}

To pass custom objects you can have a look at this link

http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/

How do I pass data between Activities in Android application?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);

Access that intent on next activity:

String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");

The docs for Intents has more information (look at the section titled "Extras").

How to pass an object from one activity to another on Android

One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method.

Actual Code:

In Your Custom Model/Object Class:

public class YourClass implements Serializable {

At other class where using the Custom Model/Class:

//To pass:
intent.putExtra("KEY_NAME", myObject);

myObject is of type "YourClass".
Then to retrieve from another activity, use getSerializableExtra
get the object using same Key name. And typecast to YourClass is needed:

// To retrieve object in second Activity
myObject = (YourClass) getIntent().getSerializableExtra("KEY_NAME");

Note: Make sure each nested class of your main custom class has implemented Serializable interface to avoid any serialization exceptions. For example:

class MainClass implements Serializable {

public MainClass() {}

public static class ChildClass implements Serializable {

public ChildClass() {}
}
}

How to pass a value from one Activity to another in Android?

You can use Bundle to do the same in Android

Create the intent:

Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();

//Create the bundle
Bundle bundle = new Bundle();

//Add your data to bundle
bundle.putString(“stuff”, getrec);

//Add the bundle to the intent
i.putExtras(bundle);

//Fire that second activity
startActivity(i);

Now in your second activity retrieve your data from the bundle:

//Get the bundle
Bundle bundle = getIntent().getExtras();

//Extract the data…
String stuff = bundle.getString(“stuff”);

How to pass String from One Activity to another Activity and use it in intent for Dialpad and Email

Try this:

Declare the variable Globally

public class gender_details extends AppCompatActivity {


private TextView tutor_email,tutor_mobile;

private ImageView img;

String tutor_email_txt;

String tutor_mobile_txt;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.profile_details);


Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_details); // get the reference of Toolbar

toolbar.setTitle(getIntent().getStringExtra(KEY_NAME));

toolbar.setLogo(R.drawable.ic_person_black_24dp);

setSupportActionBar(toolbar); tutor_email_txt = intent.getStringExtra(EMAIL);

tutor_mobilee_txt = intent.getStringExtra(MOBILE);

// Setting values


TextView Email_Txt = (TextView) findViewById(R.id.tutor_email);

Email_Txt.setText(tutor_email_txt);


TextView Contact_Txt = (TextView) findViewById(R.id.tutor_contact);

Contact_Txt.setText(String tutor_mobile_txt);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.toolbar_menu, menu);

return true;

} // Activity's overrided method used to perform click events on menu items

@Override

public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement

// Display menu item's title by using a Toast.

if (id == R.id.action_call) {



Intent intent = new Intent(Intent.ACTION_DIAL);

intent.setData(Uri.parse("tel:"+tutor_mobile_txt));

startActivity(intent);

return true;

} else if (id == R.id.action_email) {


Intent intent = new Intent(Intent.ACTION_SEND);

intent.setType("plain/text");

intent.putExtra(Intent.EXTRA_EMAIL, tutor_email_txt);

intent.putExtra(Intent.EXTRA_SUBJECT, "subject");

intent.putExtra(Intent.EXTRA_TEXT, "mail body");

startActivity(Intent.createChooser(intent, ""));


return true;

}

return super.onOptionsItemSelected(item);

}

@Override

public void onBackPressed() {

Intent intent = new Intent(gender_details.this, MainActivity.class);

startActivity(intent);

}

}


Related Topics



Leave a reply



Submit