emeraldhieu
emeraldhieu

Reputation: 9439

Call a activity's function from the other

I have two activities A and B. Activity A starts activity B. Now I'm in B, how to call A's function?

Upvotes: 0

Views: 1755

Answers (2)

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

Well, you can't...but what you can do is this:

  1. You can create a BaseActivity with the functionality you want in both A and B, and then have both A and B extend this class.
  2. You can pull the common code into its own class, and have it be a member variable on both A and B.

The first way is probably preferred, as it may be difficult to get your common code class into the activities.

Upvotes: 1

Alex Gitelman
Alex Gitelman

Reputation: 24722

Unless function is static you are out of luck. I suggest that you place necessary functions in Application object or make them static.

Note that activity not on top of the stack can be destroyed at any time.

Upvotes: 4

Related Questions