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.
Related
Can i call second activity from current activity without use intent? and why use intent, why not call second activity direct from the first activity?.
What does intent do in android?
Because to start any new Activity Android must have to go through the life cycle of an Activity. So it is necessary to use intent.
http://developer.android.com/reference/android/content/Intent.html
you can't ,see this link android is designed in this way to launch another activity
Intent says to Android you need open a new activity. Always when you need open a new Activity, you need "alert" the S.O before. Because of this we use Intent.
Activities doesn't work like a simple class, when we just instantiate.
This link will help you
http://www.vogella.com/tutorials/AndroidIntent/article.html
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.
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 one activity 'A' which extends mapActivity which calls service, and one more Service 'B' which extends service. I'am getting Latitude and Longitude in service 'B'. Now i need call the method in Activity 'A' and pass the Latitude and Longitude. In that method I have code to display Location. Thanks in advance..
See http://developer.android.com/reference/android/app/Activity.html#StartingActivities
The A activity should start the B activity using startActivityForResult() method
The B activity should set the data to return (latitude and longitude) with setResult() before doing finish()
The A activity must override onActivityResult() to retrieve data from B
You have to update the activity some service. Class that extends the activity is activity.So in your case B is Service
Try to use runnable and handler to update activity from service
Check here
Use intent to call one to other
Intent i= new Intent (this,B.class);
startActivity(i);
Its an easyway to call
Intents are used for conveying short message between activities . use Intent and put extras for starting activity from service u need to add extra flags also to start activity from service.
here is reference
http://developer.android.com/reference/android/content/Intent.html
You are using one Activity and one Service, not two activities, and you want interaction between activity and service. To do so, use BoundService for the same.
See below link for more reference:
http://developer.android.com/guide/components/bound-services.html
Alternatively you can define A messenger in Activity, and can send message from service to activity.
Create that class object and call the particular method
Using intents is the easiest and the best way. In Activity A have this code to create intent:
Intent intent=new intent(this,B.class);
/After the data which needs to be sent to activity B is Processed/
/If you want to pass data/
intent.putExtra("Data", data);
startActivity(i);
In Activity B recieve it as:
Intent intent = getIntent();
String text = intent.getStringExtra("Data");
You should not create an instance of the activity class. It is wrong. Activity has ui and lifecycle and activity is started by startActivity(intent)
You can use startActivityForResult or you can pass the values from one activity to another using intents and do what is required. But it depends on what you intend to do in the method.
May i know the exact requirement, why you require to call another activity method ??
Is it possible to create implicit intent to call our own activity? If possible is it useful or better option is Explicit Intent?
please explain your situation more...
If a single Activity is there and you want to call the same activity again from this to have any refresh sort of thing...
Then its not a good idea...
All the views can be updated without calling the same activity again.
And if new view is to be generated then use another activity