How to Pass a Value from One Activity to Another in Android

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 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 the values from activity to another activity

Send value from HomeActivity

val intent = Intent(this@HomeActivity,ProfileActivity::class.java)
intent.putExtra("Username","John Doe")
startActivity(intent)

Get values in ProfileActivity

val profileName=intent.getStringExtra("Username")

How to pass values from one activity to another in android studio

You can set up intents when you are starting a new activity. Intents are a way to pass values from your current activity to the next and I'd suggest you learn the basics uses for them. Here's official documentation you can check out: https://developer.android.com/guide/components/intents-filters

When you start the other activity you can code the Intent like this:

//Code to start an activity
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("id", "YOUR_VALUE1");
intent.putExtra("title", "YOUR_VALUE2");
startActivity(intent);

On the SecondActivity.class you will get can then assign your value to a variable:

//Code on your second activity
public class FollowersActivity extends AppCompatActivity {

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

Intent intent = getIntent();
String id = intent.getStringExtra("id");
String title = intent.getStringExtra("title");

//id will equal "YOUR_VALUE1"
//title will equal "YOUR_VALUE2"

Passing of value from one activity to another in android

Mistakes you made:

1) Why are you trying to start activity twice?

startActivity(myIntent);   // this is OK
HomeActivity.this.startActivity(myIntent);

2) To pass entered text from one activity to another, you can use putExtra() method of Intent, for example:

myIntent.putExtra("SearchText", outlet_no);

So your complete code would be:

 Intent myIntent = new Intent(HomeActivity.this, StoreActivity.class);   
myIntent.putExtra("SearchText", outlet_no);
startActivity(myIntent);

3) To receive value which you have passed from first activity:

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String outlet_no= bundle.getString("SearchText");

4) SharedPreferences:

FYI, SharedPreference is used to save small amount of data which can be reference and used later across application.

You should refer below links:

  1. SharedPreferences
  2. Intent
  3. Intent and Intent Filters

Android: Pulling variable from one activity to another and set a textview.

You can pass primitive data from one activity to another as follows.

Define constant which will be used as Key to pass data.

public static final String EXTRA_YOUR_KEY = "EXTRA_YOUR_KEY";

Add the value as key-pair to the Intent which you are using to start new activity.

// code snippet for first activity
long myId = 10;
Intent activityIntent = new Intent(activity-context, ClassNameOfNewActivity.class);
activityIntent.putExtra(EXTRA_YOUR_KEY, myId);
startActivity(activityIntent);

Next, in the onCreate() of new activity, extract the passed value as

// code snippet from second activity's onCreate method
Intent intent = getIntent();
long id = intent.getLongExtra(ClassNameWhichContains.EXTRA_YOUR_KEY, 0);

I have used long as example since you are trying to pass in a long variable. Similarly, you can work out the other primitives.

And for passing custom objects between two activities you can refer this.

Hope this helps.

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 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 !



Related Topics



Leave a reply



Submit