2

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?

ab11
  • 19,770
  • 42
  • 120
  • 207

7 Answers7

4

If you use a custom application class, you can store information that will be kept between the activities.

See a tutorial here for instance.

Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
2

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.

Rajath
  • 11,787
  • 7
  • 48
  • 62
2

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.

Guillaume Brunerie
  • 4,676
  • 3
  • 24
  • 32
  • Why do I need to unset it in onDestroy()? The first activity is my main activity, so its onDestroy() means the application is being closed. I assume in this case I'm not concerned with memory leaks? – ab11 Apr 22 '11 at 14:16
  • No, the first activity is destroyed, but not necessarily the whole application. If you do not unset the variable in `onDestroy()`, you will keep the whole activity in memory (even if this particular instance of this activity does not exists anymore) and you will leak a lot of memory. This is exactly the problem described [here](http://developer.android.com/resources/articles/avoiding-memory-leaks.html), see in particular the first point of the summary. – Guillaume Brunerie Apr 22 '11 at 14:21
  • From reading this article, it seems that the concern is static references to variables which refer to the Activity? Not all statics have to be explicitly released in the onDestroy(). For example, if somewhere in my program I define a constant: public static final String SOME_CONSTANT = "someconstant", I am not concerned about that creating memory leaks. Is this correct? – ab11 Apr 22 '11 at 14:44
  • Yes, I was talking about putting the whole first activity as a public static variable. If you are sure that the value does not contains a reference to a Context (or an Activity, a View, a Drawable, …) you should be fine. – Guillaume Brunerie Apr 22 '11 at 14:49
2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you, this seems to be the correct solution. I chose marvinlabs' answer because he responded first. – ab11 Apr 22 '11 at 14:45
1

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.

james
  • 26,141
  • 19
  • 95
  • 113
  • Is it sufficiently safe to check that the object exists by: Activity.complexObject != null? – ab11 Apr 22 '11 at 14:18
  • Yes, but only if you initialize your complexObject to null as you define it as a field: `public static MyComplexObject complexObject = null;`, otherwise you will have to check if `null == complexObject`. – james Apr 22 '11 at 14:21
0

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.

-1

Use getParent() from new activity and call parent's method

Android Activity call another Activity method

Community
  • 1
  • 1
Priyank
  • 10,503
  • 2
  • 27
  • 25
  • I’m not sure this will work, the doc says “Return the parent activity if this view is an embedded child.“ which does not seem to be the case (but I may be wrong). – Guillaume Brunerie Apr 22 '11 at 14:24
  • from the docs: `getParent(): Return the parent activity if this view is an embedded child.`. I don't think this will be what works for this problem – james Apr 22 '11 at 14:24
  • This will not work. That's not the kind of parent you're looking for. – zeh Dec 15 '11 at 20:33