In onActivityResult() why do we have a intent as parameter ? If it was the case of sending data from one activity to another ,can't data be sent via bundle ?
Help me !!
The document says,
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.
Intent is used in Activity's transition.
For example, Intent is used when calling Activity_B from Activity_A.
Also, it is used when returning from B to A. That's all.
The Intent is for receiving data back in the onActivityResult(int, int, Intent) method of your calling Activity. And, yes, a Bundle can be a part of this Intent.
Think of an Intent as Message that you can send all over the android System between the android Components (Activity , BroadcastReceiver , Service , ContentProvider ) .
and this Intent (Message) need to have some content inside , and think of the Bundle as the content of your Message that you are sending to the other component .
Hope that Helps
whenever we start any activity for result by calling startActivityForResult() from current activity, it is must that started activity will return back with some response, and this response will warped in intent object.
Yes you can do this, but it will being complex when you application will getting large means you are heavily using Bundle,
one drawback is more using Bundle it will having Key value pairs so its possible accidentally change you value by some other activity.
Related
I'm just curious to find if there is any difference for starting activity between these two types
StartActivity (typeof (MainActivity));
StartActivity (new Intent (this,typeof (MainActivity)));
Consider the first as a shortcut for the second.
If you dont want to share any data to the new activity you can call the first.
In other cases you may want to pass an Id or any other data to use in the target activity through a Bundle with the PutExtra methods of the Intent.
I am using intent to transfer data.
When I try the following code,
Intent intent=getIntent() or Intent intent=getIntent(DrawerActivity.this, null)
Exception will occur.
How can I use intent to put data but do not jump to another activity?
If you want to put data somewhere on the app and not use it instantly, then You can store some values on SharedPreferences, especially if it's not huge amount of data
Intent is a mechanism to pass data from one activity to another.
If you have to store some data in a place and then later send it to another activity, create a bundle, store data in it and then when you have to go to next activity then create a intent, set the class of the next activity and in putExtras pass the bundle and the data will be sent to that activity.
It is depends of situation, you can use PendingIntent or you can create Bundle and put in it what values you want and when you want to start the activity then pass the Bundle as desctibed here.
In my application I want send data from activity to another activity and for this I don't want use Intent.
I know I can use this code :
Intent intent = new Intent(context, Activity.class);
intent.putExtra("name", value);
startActivity(intent);
and for this I don't want use SharedPreferences again!
Do you know other way for send data from activity to another activity without 2 above ways?
If I understand correctly, you are looking for a way to pass data from one Activity to another Activity without using either SharedPreferences or the SQLite database. Using an Intent is OK but not with startActivity(). I assume that this also goes for startActivityForResult().
Two Activitys of the same application will as a rule not be in the foreground at the same time, so LocalBroadcastManager won't be very helpful in this case. Simply "sending" won't be possible, one needs some kind of middle man.
You could look into EventBus libraries, e.g. Otto EventBus
But maybe you'd prefer to work with some type of POJO to hold the data, and perhaps some Observer which the Activitys could query. Such a class could be implemented as a field in a custom class extending Application.
I have 2 activities - A & B. Activity B is started via activity A, but it needs a piece of information (a String) to startup. Right now I am using putExtra & getExtras methods of Intent class to pass the data around. In any way can I enforce the requirement that the intent that is used to start activity B should always have a String stored using a particular key ?
You can't enforce it, but:
You can create a static method on activity B that is responsible for starting instances of activity B, and have that method take your String parameter. That method would be responsible for building the Intent, putting in the extras, and calling startActivity(). So long as the rest of your code uses this method, you will always have your extra.
You can always validate that the extra exists in onCreate()/onNewIntent().
But there's no way you can teach Android to automatically reject an Intent that is missing some extra.
As you can see from the documentation the way to start an Activity to have data passed back is like so:
Start the Activity using the call startActivityForResult(Intent intent, int requestCode).
In the started Activity call setResult(int resultCode, Intent data).
Implement onActivityResult(int, int, Intent) on the calling Activity.
The question is: is their a pattern or design consideration for using an Intent in this situation where as everywhere else inter-activity data is represented as a Bundle?
By passing back an intent rather than merely a bundle, the caller can receive something with which to directly start another activity, service, etc. Though that may not be required in all (or even most) cases, it still allows a bundle to be delivered in the intent's extras.
Until I get a more detailed answer the only thing I can think is that this has been done specifically to give the called Activity increased control over the resulting action.
The only scenario I can see where it would be better to be done in this way is if the called Activity was in another sandbox and could construct the Intent using an explicit Class reference rather than through the use of an Action locking the resulting call to that of the specified explicit Activity.