Passing a Bundle on Startactivity()

Passing a Bundle on startActivity()?

You have a few options:

1) Use the Bundle from the Intent:

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);

2) Create a new Bundle

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);

3) Use the putExtra() shortcut method of the Intent

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);



Then, in the launched Activity, you would read them via:

String value = getIntent().getExtras().getString(key)

NOTE: Bundles have "get" and "put" methods for all the primitive types, Parcelables, and Serializables. I just used Strings for demonstrational purposes.

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").

Android How to send a Bundle to another Activity?

First create the data bundel and attach it to intent

Intent mainIntent = new Intent(getApplicationContext(),XXXX.class);
Bundle dataBundle = new Bundle();
dataBundle.putString("keyid","thevalue");
mainIntent.putExtras(dataBundle);
startActivity(mainIntent);

to get this bundle in the XXXX Activity

//getting the datbundel from other activity incoming
Bundle extras = getIntent().getExtras();
String a=extras.getString("keyid");

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 data from one activity to another using bundle - not displaying in second activity

Two ways you can send the data. This is how you are sending it at the moment. And there is nothing wrong with it.

//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("VENUE_NAME", venueName);
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);

In you code (second Activity) however, you are referring to the key in the Bundle as MainActivity.VENUE_NAME but nothing in the code suggests that you have a class that returns the value as the actual key name send with the Bundle. Change your code in the second Activity to this:

Bundle bundle = getIntent().getExtras();

//Extract the data…
String venName = bundle.getString("VENUE_NAME");

//Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(venName);

You can check in your second Activity if the Bundle contains the key using this and you will know that the key is not present in the Bundle. The correction above, however, will get it working for you.

if (bundle.containsKey(MainActivity.VENUE_NAME))    {
....
}

Android: How to get the Bundle in an intent?

You have to use getIntent() to retrieve the intent used to create the activity and you can then use getExtra on the intent.



Related Topics



Leave a reply



Submit