I am using Android Studio and have a method in my MapsActivity that I need to access from another activity. I think using intent is the best way, but I do not know how to.
This is probably a bad idea. The activity which you would like to invoke the method could be stopped while you are on the second activity, for example.
If you need the method, instantiate the class. If the method is refreshing some view on it's activity, you could maybe use "onActivityResult". Otherwise you could create an abstract class and call the method.
You can do what you want using onActivityResult.
When you go back to the first Activity you would call startActivityForResult. Then you put the data in a new intent when the user clicks the marker and finish similar to:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
and then handle the result in your second activity.
More details are here:
https://stackoverflow.com/a/10407371/4888701
EDIT: Based on your comment, and as Felipe Martins pointed out,startActivityForResult() can work for you.
Use bundle.putDouble to add lattitude and longitude to the bundle and retrieve it later in onActivityResult().
Use this for reference: https://stackoverflow.com/a/25642483/4941959
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.
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 ??
I want to call onCreate(Bundle cicici); from other class then i am getting "NullPointerException", so please guide me how can i call the onCreate() from another class.
There is only one way in which onCreate can be called, by starting an Activity, since onCreate is as part of Activity life cycle.
startActivity(new Intent(presentActivity.this, NextActivity.class));
if you want to call onCreate in order to actually present a new screen, then you need to create a the new Activity using the android framework style.
Ingredients:
1- An event to call your new activity( ie. onClickListener of a Button or list triggered)
2- On the event you need to create an Intent with the reference of the current activity and a class reference of your new Activity, example:
Intent intent =new Intent(CurrenActivity.this, MyNewActivity.class);
3- You need to call this activity depending on what you'll need you use startActivity or startActivityForResult, the last is use when you expect a response from your activity.
You can also refer to Android documentation Common Task, let us know if its helpful
It depends what you want to do in the second activity. If you want to create a simple task you can always use dialogs and you can show them inside your activity.
Or, on a second thought, you can hide some of your views and enable others but I guess that's not an orthodox solution :)
I want to find out which activity started my activity in android. I can get the intent that started the activity using getIntent() but I am not able to find out which activity started the intent in the first place.
Use getCallingActivity()
Note: if the calling activity is not expecting a result (that is it did not use the startActivityForResult(Intent, int) form that includes a request code), then the calling package will be null
You can use ActivityCompat.getReferrer() for this purpose.