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)
Related
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");
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.
hi i am passing the latitude,longitude and address using a bundle to another activity. in another activity i am retrieving data from the bundle. but when i click the button on which bundle is used the app crashes. i cant post the logcat as nothing comes in ddms. i m using the device debug mode. heres my code:
Bundle b=new Bundle();
b.putString("latitude", lat+"");
b.putString("longitude", lon+"");
b.putString("address", result1);
Intent i=new Intent(Cortes.this,Display.class);
i.putExtras(b);
startActivity(i) ;
i am writing this in listener of button and Display is the another activity. in that activity my code is:
Bundle b=this.getIntent().getExtras();
latitude=b.getString("latitude");
longitude=b.getString("longitude");
address=b.getString("address");
i am displaying db in that activity in a tablelayout.
cursor for that i have written in another activity named Display.in cursor i am using the values which i am passing to another activity
pls help
try this
Bundle b = getIntent.getExtras();
or
Bundle b = ClassName.this.getIntent.getExtras();
retrieve data like this:
Bundle var_name = getIntent().getExtras().getBundle(key);
If you use a custom Bundle, it seems there is some requirements, according to the doc:
The keys must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
The common way I know to share data between activities is to use the default Intent bundle to store them, like this:
Intent i=new Intent(Cortes.this,Display.class);
i.putExtra("latitude", lat+"");
i.putExtra("longitude", lon+"");
i.putExtra("address", result1);
startActivity(i);
Then, you retrieve the infos like this:
Intent i=this.getIntent();
latitude=i.getStringExtra("latitude");
longitude=i.getStringExtra("longitude");
address=i.getStringExtra("address");
I have some values and want to pass with activity so that I can show in TextViews of Activity, I am unable to understand such a concept, so what should I do?
Simply I want to make constructor but unable to understand it that how will it be done, I am new to android programming, so needed help.
Start by reading the documentation at developer.android.com about intents and intent extras.
In android if you launched an activity there is a method called onCreate execute automatically.You can send values using Intent to the activity and can retrieve them in the Activity
The Activity (sub)class must have a default constructor without any parameters so that the system can instanciate it at run time.
To pass "parameters" to activities, you need to use the extra bundle of the intent.
Intent i = new Intent(this, MyActivity.class);
i.putExtra("com.sample.MyParameter", 666);
startActivity(i);
See Starting An Activity
If you want to pass one value then you can use intent but if you want to pass multiple values then using "Bundle" is best way.
Bundle bundel = new Bundle();
bundel.putStringArray("key1",strings);
bundel.putStringArray("key2",stringsofids);
bundel.putString("key3", str31);
bundel.putStringArray("key4",stringsbakup);
bundel.putString("key5", str1);
bundel.putString("key6", str4);
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