Passing your own objects between activities - android

I am a newbie on Android development and how data pass from one activity to another is still unclear for me.
Is it a good practice to pass your own objects between activities?
Which size could they have?
EDIT:
Maybe I did not explained it clearly. I know how to pass data between activities. The question is: Is it a good practice or should I better obtain data from SQLlite database? In that case, which is the maximun size I can pass to have a good performance?

Sending object is same as sending pre defined objects like String or variables,it can be done by binding your object to Intent by using putParceble() or putSerializable() methods directly on intent or by binding object to a bundle object.
But u have to make sure that your class implement either Parcelable or Serializable.
like here:
UserDefined myObject=new UserDefined();
Intent i = new Intent(this, Activity2.class);
Bundle b = new Bundle();
b.putParcelable("myObject", myObject);
i.putExtras(b);
startActivity(i);
And in receiving activity:
Bundle b = this.getIntent().getExtras();
myObject = b.getParcelable("myObject");
you can also send object without using Bundle:
Intent i=new Intent(PicActivity.this,PostPhotoActivity.class);
i.putExtra("myObject", myObject);
startActivity(i);
on receivingActivity:
UserDefined myObj=(UserDefined)getIntent().getParcelableExtra("myObject");
In Android parcelable is prefered instead of serializable.

Related

What is meant by getIntent().getExtras()?

I am new to Android development and I have come across something new while using Intent as getIntent().getExtras().
Can anyone please explain me how can we write getIntent().getExtras(),cause till now what I know is we can call method by creating object of that particular class but here we are call method getExtras() by using getIntent()method.
getIntent().getExtras() is used to get values from intent that are stored in bundle.
Intent class is used to switch between activities. But sometimes we need to send data from one activity to another. So, at this particular moment we need to set some values to intent that can be transferred to destination activity. We can achieve this by the following code -
Bundle bundle = new Bundle();
bundle.putString("key1","someValue");
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putStringExtra("key","value");
intent.putExtras(bundle);
startActivity(intent);
Now, in the second activity we can get the value of "key" so we can use that in second activity. To do so, we use the getIntent().getIntent can store a Bundle. Let's see an example -
Intent intent=getIntent();
Bundle valueFromFirstActivity = intent.getExtras();
String valueOfKey = intent.getStringExtra("key");
String valueOfKey = bundle.getString("key1");
So this way, one can get values from activities. Bundle is a class that can hold values within itself and that instance of bundle can be given to intent using putExtras(). It is quite helpful in transferring the custom array list.
we can call method by creating object of that particular class
getIntent() is a method that returns an Intent object. When you call getIntent().getExtras(), first an Intent object is returned and then getExtras() method is invoked.
This way of calling is referred to as method chaining(Fluent Interfaces)

Pass object of type from library through intent to another activity?

I have a ComponentList object from the ical4j library, and I'm loading it from a url in an initial activity and then passing it to my main activity where I'll set up the GUI with data from it.
I can't figure out how to actually pass it with an intent, however. I can convert it into a string and that works (using componentList.toString()), however I want it to still be a ComponentList object. I've read some about using a parceable, but this isn't a class I've written so I can't go into the code and have the ComponentList object implement parceable. Is there a good way to pass an object from the ical4j library, in this case ComponentList, to another activity?
Looking at the API Docs it implements Serializable. So, you should be able to send it using an Intent:
Intent i = new Intent(this, MainActivity.class);
i.putExtra("componentList", componentList);
startActivity(i);
Then, in your MainActivity's onCreate():
Bundle extras = getIntent().getExtras();
ComponentList componentList = (ComponentList) extras.getSerializableExtra("componentList");

What is the difference between a Bundle and an Intent?

An Intent is a passive data structure that carries information from one Activity to another. An Intent is also capable of holding data in the form of name-value pairs (via putExtra()).
But while overriding the onCreate() method we pass a Bundle as the parameter, which ultimately also holds values in the form of name-value pairs and is able to store information with the help of onSaveInstanceState().
In such a scenario why do we need both and what differentiates the two?
I suppose I have led you guys into a misbelief that I have misunderstood what an Intent is:
When I said "An Intent is a passive data structure that carries information from one Activity to another", what I intended to point out was that even an Intent can carry information (other than the context and action description) with the help of putExtra() method. Why do we need to use a Bundle then?
I think you already have understood what a Bundle is: a collection of key-value pairs.
However, an Intent is much more. It contains information about an operation that should be performed. This new operation is defined by the action it can be used for, and the data it should show/edit/add. The system uses this information for finding a suitable app component (activity/broadcast/service) for the requested action.
Think of the Intent as a Bundle that also contains information on who should receive the contained data, and how it should be presented.
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.
Intent facilitate communication between components.Intent is the message that is passed between components such as activity.
that can be used intent.putExtra(Key,value) and intent.putExtra(Bundle)
Intent intent = new Intent();
intent.setClass(this, Other_Activity.class);
// intent.putExtra(key,value)
intent.putExtra("EXTRA_ID", "SOME DATAS");
startActivity(intent);
Using Bundle : For example
Bundle bundle=new Bundle();
bundle.putString("Key","Some value");
intent.putExtras(bundle);
startActivity(intent);
Call the bundle in another activity :
Bundle extras=getIntent().getExtras();
extras.getString(key);
I really don't know from where you got this definition for Intent, but as an 'Intent' definition
An intent is an abstract description of an operation to be performed.
It can be used with startActivity to launch an Activity,
broadcastIntent to send it to any interested BroadcastReceiver
components, and startService(Intent) or bindService(Intent,
ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding
between the code in different applications. Its most significant use
is in the launching of activities, where it can be thought of as the
glue between activities. It is basically a passive data structure
holding an abstract description of an action to be performed.
So Intent is an action to link to new (Activity, Service, BroadCastReceiver)
In Intent you will find a definition for Extras
extras -- This is a Bundle of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.
So that means Extras in the Intent is an object of A Bundle
Going to Bundle as you mentioned it is a carrier for data from one Intent to another and is a map of Key-Value variables.

Make activities communicate [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I pass data between activities in Android?
I have three activities in my application and each activity is depending on another. I'm currently using static variables to pass objects and values between these activities. Problem with this is that it get's very confusing fast and it's hard to keep track of when I assigned that global variable a value etc. I'm thinking of implementing an interface between these activities to make the code clearer and easier to understand. Thing is, i'm not entirely sure this is the best way to go so any help or advice would be great.
use putExtra to send info to another activity
send:
Bundle bundle = new Bundle();
bundle.putString(“name″, “username”);
Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
Receive:
Bundle bundle = this.getIntent().getExtras();
String data = bundle.getString(“name″);
data = username
I believe what you want is the Intent.putExtra() method. There are several methods depending on what kind of data you want to pass. See the documenation here.
to pass data:-
Intent i = new Intent();
i.putExtra("key", "data");
startActivity(i);

working with android intents how to pass arguments between father and the intent h in

Assuming i want to open another activity from my current activity and i want to pass arguments such as in my case difficulty level how do I do it?
newGameButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(countryCityGameMenu.this,GameScreen.class);
startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE);
}
});
is there a way to pass those arguments in the calling ?
can someone show an example explaining
what send activity should do
what the new born activity should do
As ben already mentioned you can add this data to the intent inside an extra bundle.
An extra bundle stores data as a key value pair of native java data types.
You can add data to the intent via the putExtra methods.
In the new Activity you can retrieve this data via the getExtra methods of the Intent. For example the getStringExtra method.
To get the intent that started the current activity just use the getIntent() method from activity.
You have to use extras like this:
i.putExtra(varName, value);
Not a big fan of this approach... but unfortunately it sends to be the only way to do it in android...
fine.. U can also use StartActivity(intent); Thats enough to pass

Categories

Resources