Simple Example for Intent and Bundle

Simple example for Intent and Bundle

For example :

In MainActivity :

Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra(OtherActivity.KEY_EXTRA, yourDataObject);
startActivity(intent);

In OtherActivity :

public static final String KEY_EXTRA = "com.example.yourapp.KEY_BOOK";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String yourDataObject = null;

if (getIntent().hasExtra(KEY_EXTRA)) {
yourDataObject = getIntent().getStringExtra(KEY_EXTRA);
} else {
throw new IllegalArgumentException("Activity cannot find extras " + KEY_EXTRA);
}
// do stuff
}

More informations here :
http://developer.android.com/reference/android/content/Intent.html

Intent and Bundle Relation

Sometimes you need to pass only a few variables or values to some Other Activity, but what if you have a bunch of variable's or values that you need to pass to various Activities. In that case you can use Bundle and pass the Bundle to the required Activity with ease. Instead of passing single variable's every time.

What is the difference between a Bundle and an Intent?

From the source of Intent class, there really is no difference between the two. Check below code from Intent class:

    public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}

And

    public Intent putExtras(Bundle extras) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putAll(extras);
return this;
}

So I think, only difference is ease of use.. :) for 1st, you don't need to create your bundle explicitly.

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.

Intent.putExtra(String,Bundle) vs Intent.putExtra(Bundle)

I think you mean putExtra(String, Bundle) vs putExtras(Bundle) (with s).

The first adds the bundle as the value for the key you provide. The bundle is simple an object value.

The second adds all the key/value pairs from the provided bundle to the intent. In this case the content of the bundle will be added to the intent, not the bundle itself.

Think of them as in Map interface:

Map.put(String key, Object value)

vs

Map.putAll(Map anotherMap)

How to use putExtra() and getExtra() for string data

Use this to "put" the file...

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String strName = null;
i.putExtra("STRING_I_NEED", strName);

Then, to retrieve the value try something like:

String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}

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 the next activity:

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

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

How do I get extra data from intent on Android?

First, get the intent which has started your activity using the getIntent() method:

Intent intent = getIntent();

If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:

String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");

android - java - pass values through intents, bundles or Parcelables?

BUT I will still have to "serialize" it, so in the end, I will also have primitives, right ?

Pretty much everything in Java eventually boils down to primitives.

To pass primitives, why should I use a bundle when passing directly in an intent will make it too ?

Either way works. Use whichever makes you feel more comfortable. However, just watch out for collisions on keys (e.g., your activity, and a some base activity of yours that you inherit from, both trying to put the same thing in the same key of the Intent or Bundle).

Why having 3 different ways to achieve one goal, by the way ?

I am going to guess that "one goal" is "to pass data from one activity to another". That involves inter-process communication (IPC), even if the two activities are in the same process, as a core OS process is involved in the routing. For pretty much everything outside of streams from a ContentProvider in standard Android, IPC means that data has to be put into a Parcel, which gets converted into a byte array for passing across the process boundary.

Parcelable represents an interface that can be added to custom classes to allow them to be put into a Parcel.

Bundle is a concrete class that implements Parcelable and represents a HashMap-like structure, but strongly typed to things that are known to be able to go into a Parcel. Bundle is more convenient than a Parcel for developers, in that it offers random access by key, where Parcel does not.

Intent extras are merely a Bundle, for which Intent exposes its own accessor methods.

For all my cases, I should forget about the other possibilities and only use Parcelable ?

AFAIK, what EpicPandaForce was referring to was the comparison between Serializable and Parcelable. Either can go into a Bundle or a Parcel. Of the two, all else being equal, Serializable is slower, because it assumes that the serialized form has to be durable, able to be read in again months or years later. Parcelable assumes that everyone is working off of the same class definition and can bypass some Serializable overhead as a result.

That being said, it's a micro-optimization.

Why are the other still there then ?

Not everything can be made to extend Parcelable, notably java.* classes. Integer, for example, is not Parcelable. Yet, we would like to be able to pass int and Integer values around. Hence, Parcel supports int, as does Bundle.

you mean that I should get it back with the R.blah thing

AFAIK, in that comment, Mr. Marconcini was referring to "id" as a general concept, not referring to R.id values specifically. For example, if you are maintaining an image cache, rather than passing around actual Bitmap objects, pass around some identifier that points back to the cache.

How to send an object from one Android Activity to another using Intents?

the most easiest solution i found is..
to create a class with static data members with getters setters.

set from one activity and get from another activity that object.

activity A

mytestclass.staticfunctionSet("","",""..etc.);

activity b

mytestclass obj= mytestclass.staticfunctionGet();


Related Topics



Leave a reply



Submit