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.
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 running into an issue with passing values to activities. I have a Title activity that launches Generating activity. Generating activity launches Play activity. Suppose I want to pass some value such as a boolean to Play activity from Title activity.
Would I have to pass it first to Generating activity and then make Generating activity pass the value to Play? Or can I pass it via putExtra() bypassing Generating activity even if Title activity does not launch Play activity directly?
An intent is a message containing data that will launch an activity. The data is only available to the target activity.
So to answer your questions:
No, you cannot pass intent data from TileActivity to PlayActivity. You must pass it to GeneratingActivity, who in turn would then pass it to PlayActivity.
There are alternatives to using intents such as:
Static/Global data - Use this with care. Because of how Android manages your process, it can be dangerous to use this approach.
SharedPreferences - This is a mechanism for persisting data. Your first activity could save the data in SharedPreferences, and the third activity could read it from SharePreferences. Because of the dangers with approach #1 above, many people take this approach in Android.
You can create a public class with a static variable :
public class Global {
public static boolean play = false;
}
You just need to call Global.play = true in your Title Activity and check the value of Global.play in your Play Activity (or other Activities based on your need)
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.
I have asked this question a few times on here already, but still am not confident enough to start throwing things into my code because I'm afraid I'll ruin the functionality it already has.
What I want to do is have a Bluetooth connection with a device that I can send/receive data to/from, and be able to access it from several different Activities. I know Intents can be used, but can anyone try to explain how intents work?
I don't understand how if I start with an Activity, how I can build an object within that Activity, then end it, and still have the object. When I call the intent in the following Activity, how do I access it?
I can not find example code of an Activity with a custom object that is passed through an intent, opened in another Activity, then passed again through another Activity, and so on. All I can find is things for string's, int's and it seems that those methods are hardcoded for the specific objects.
Any help would be greatly appreciated, thanks.
If you want to pass a custom object from an Activity to another one, your object have to implement Parcelable interface.
Here you can find an example of an object which implements Parcelable.
Once you have an instance of this object you can add it to an intent and pass it to other activity.
Let's say you have 2 activities: A and B.
If you want to send a custom object from A to B try put your parceable object in an intent and get it back in Activity B.
// Activity A
Notebook myNotebook = new Notebook(); // the "Parcelable" object
Intent intent = new Intent(A.this, B.class);
intent.putExtra("object", myNotebook);
startActivityForResult(intent);
// Activity B
// in onCreate method
Notebook notebook = intent.getParcelableExtra("object");
there is a good reason you don't find a good example of transfering complicate objects: you don't suppose to that a lot. it's possible to transfer with intent extra any object, with declare the class you want to transfer as implements Serializable, or Parcalable.
but - this is not recomended doing so, unless you have good reason.
there are unlimited ways to create "cross activities" objects (databases, singeltones, services..), and passing complicated objects in intent is heavy operation. also it makes terrible performances doing so.
anyway - if you want anyway doing so, that's the way:
this is how to pass serlizable object:
Intent intent = new Intent();
intent.putExtra(name, someInstanceOfClassWhichImplementsSerializableInterface);
this is how to acheive it on the other activity:
getIntent().getSerializableExtra(name);
about the question that you don't understand how the object still lives after you finish the activity - finish() activity is not triggering distractor!!!, but do triggers to onDestoy() callback. the system decides when it's the right time call the activities distracor , and even if it was distractor - it's not preventing from the Intent object to stay alive - because nobody said your activity is the only one holding reference to it. the intent reference held by the system for the perpose of passing it to the new activity which opened with it.
Using intent you can send serialized object which is like converting you object into byte stream. If you want to send your object as it is you can use handlers.
e.g.
create Bluetooth connection using service(android service)
define handler in you activity which will be static object
from service to send the object add to msg.obj send that msg to handler
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.