Find out which activity started my activity? - android

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.

Related

Difference on starting activity

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.

How can I use Intent to access a method from another activity?

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

Call activity direct from current activity

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

Unknown method getIntent

I am making an application on AIDE for android, and I'm using an intent to send data from an activity to a normal class.I'm using:
int level= (currentLevel*100)/scale;
Intent i = new Intent(context, caller.class);
i.putExtra("level",level);
context.startActivity(i);
in the class that sends the data ("percentage.class").
int p = getIntent().getIntExtra("level");
in the class that receives the data ("caller.class")
which gives me an error: "Unknown method getIntent()".
What can I do to fix this?
Thanks in advance!
It's easy to say in your question what is the problem. The class in which you are calling getIntent does not inherit the class Activity.
Unlike what other people are saying inheriting Activity is unlikely to give you what you're looking for. What I'm suspecting is that you're calling getIntent in a button or something like this. Since it might be wrapped inside a method that isn't directly pointing to your activity. You should "keep" a pointer to the activity.
Usually, what you are looking for should be in the context. Calling context.getIntent might work if your context is the thing I "believe" it should be. Show more to give us a better idea of what is going on. Because since getIntent is calling from the activity. getIntent is the same as writing this.getIntent but Java implicitely calls function on this and then on the global scope (the thing you import).
If you want to avoid this problem, alway call it from this and when you're calling from within a Handler, you can keep references in your class to the current activity. I'm not so sure but on some object, you should have a function getActivity that will return the activity in which they are located.
you could have something like this. obj.getActivity().getIntent()...
Check this out: What does getActivity() mean?
The getIntent() method belongs to the Activity class. You will get this error if your class does not extend Activity. Try extending Activity and see if it works.

How to know what Activity has started the current Activity

How can I know what Activity has started the current Activity? Is there any specific method like, i.e. getIntent().getCallerActivity() or the only way is saving some information within the Intent using putExtra()?
Thanks.
There's getCallingActivity which you can use to get the name of the calling activity if you started the sub-activity with startActivityForResult.

Categories

Resources