Reputation: 20090
This is a pretty simple question, but I have been unable to find anyway to accomplish what I am trying to do...
I want to launch a new Activity to display some complex information. Because of the complexity, it is undesirable to serialize the information into the intent's parameters. Is it possible for the the new Activity to get a reference to the launching activity, so it can call its methods?
Upvotes: 2
Views: 385
Reputation: 26271
You can make your complex objects public
and static
in ActivityA
, and access them in ActivityB
like this:
MyCustomObjectType complexFromA = ActivityA.complexObject;
this will work, however while in ActivityB
, you can't always be sure that static objects from ActivityA
will exist(they may be null) since Android may terminate your application.
so then maybe add some null checking:
if(null == ActivityA.complexObject) {
//go back to ActivityA, or do something else since the object isn't there
}
else {
//business as usual, access the object
MyCustomObjectType complexFromA = ActivityA.complexObject;
}
You could also use a Singleton object which extends Application
. You would have the same problem when Android terminates your application. always need to check if the object actually exists. Using the Singleton extending Application
approach seems to be the more organized way - but adds more complexity to implementation. just depends what you need to do and whatever works for your implementation.
Upvotes: 1
Reputation: 5759
You should create a separate class that both the activities can use.
public class HelperClass{
public void sharedFunction(){
//implement function here
}
}
I would recommend staying away from static variable in android. It can cause some unexpected behavior.
Upvotes: 0
Reputation: 10623
Use getParent()
from new activity and call parent's method
Android Activity call another Activity method
Upvotes: -1
Reputation: 1006674
Is it possible for the the new Activity to get a reference to the launching activity, so it can call its methods?
Please do not do that. Android can and will destroy activities to free up memory.
Complex information like you describe should not be owned by an activity. It should be held in a central data model, like you would in any other application. Whether that central data model is mediated by a Service
or a singleton or a custom Application
object depends a bit on the type of data, caching models, risks of memory leaks, and so on.
Upvotes: 2
Reputation: 28541
If you use a custom application class, you can store information that will be kept between the activities.
See a tutorial here for instance.
Upvotes: 4
Reputation: 5371
You can add a public static field to the first activity containing this
(the first activity).
But beware that the first activity could be destroyed by Android while you are using the second activity, so you will have to implement a fallback method if the first activity is destroyed.
And don’t forget to unset the public static variable in the onDestroy()
callback of the first activity or you will leak memory.
Upvotes: 2
Reputation: 11926
The lifetime of an Activity cannot be depended upon. In this case, one way of sharing data is to have a singleton which holds the data to be shared between the two activities.
Upvotes: 2