Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get "calling" Activity instance from called Activity?

I have a Contact Activity which is derived from ListActivity, which displays list of contacts, and on click of item, a new Activity Message Activity derived from ListActivity is initialized.

Now I know, I can pack some information in Bundle and pass it before creating activity, but is there a way I can get instance of "ContactActivity" in onCreate method of "MessageActivity"?

like image 823
Srikanth Naidu Avatar asked Jul 09 '10 14:07

Srikanth Naidu


People also ask

How do you call an activity from another activity?

Start the Second Activity To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .

How do I get call activity?

If you start the activity with startActivityForResult(Intent, int) , then you can get calling activity by getCallingActivity(). getClassName() .

What is the instance for activity?

Activity instances are always created by the Android system. This is because a lot of initializations have to be done for the activity to work. To create a new activity you call startActivity with an Intent describing the activity to start.

How do you get a response from an activity in Android?

startActivityForResult(Intent intent,int requestCode) will give the response from second activity to first activity as a result.


2 Answers

Yes there is, you can do a workaround. In your message Activity declare a static attribute of the type of your contact class, then you set that attribute with the selected contact when the list is clicked and then you start your message activity. When onCreate is executed in your message activity you can use that attribute.

like image 63
Alejandro Winkler Avatar answered Nov 11 '22 05:11

Alejandro Winkler


No, sorry, there is no built-in means for you to get at the activity that called startActivity() for your current activity. After all, the original activity might not be in your application (e.g., home screen).

like image 24
CommonsWare Avatar answered Nov 11 '22 04:11

CommonsWare