How to call methods of another Activity - android

I am using 3 classes A, B and C. In class A I created a method clickButton() and In class B I used onClick() for a button. while clicking on the button it has to call the method clickButton() in Class A, and inside the clickButton() i wrote intent for initiating the class C.
The problem is I couldn't able to call the Class A method in class B.

You generally don't want to touch another Activity directly. The Android design paradigm represents a largely separated viewpoint between different activities, and so instead of directly calling methods on a class A, you will send it an Intent or message or something. If you have a utility method, you should consider moving them into a shared class and making them static. If you have something that needs to be performed in the background or shared persistent store, you should consider moving to a service or content provider.

Related

Pass interface between activities in intent - interface fails to be Serializable or Parcelable

I want to pass an interface from 1st activity to 2nd activity.
I want to initiate methods from the interface from the 2nd activity which will affect the 1st activity.
I'm well aware that it's very overkilling not using the onActivityResult mechanism, and that it might not be good programming, but roll with me please.
Here's the issue - my interface can't implement Serializable / Parcelable since interface can't implement another class.
This is my interface :
public interface ITest {
void onSuccess(String text);
}
But, i can't start my activity with this interface since it's not Parcelable.
intent.putExtra("testInterface", new ITest() {
#Override
void onSuccess(String text) {
}
}
Obviously, i receive a compilation error :
Cannot resolve method 'putExtra(java.lang.String, ITest)'
You cannot "pass an interface". An "interface" is an abstract thing. What you want to pass is a concrete implementation of that interface, ie: an object (that happens to implement your interface). When you instantiate your "interface" (in your example, like this:
intent.putExtra("testInterface", new ITest() {
#Override
void onSuccess(String text) {
}
}
you are actually creating an instance of an anonymous class that implements the interface ITest. To pass this in an Intent you would need to make this class also implement Parcelable or Serializable.
However, even if you did that, it would not solve your problem. What you need to understand is that you can't pass objects (instances) by putting them as "extras" in an Intent. When you do that, Android actually serializes and then deserializes the object so that you end up with 2 objects, one is a serialized/deserialized copy of the original.
If you want ActivityB to communicate with ActivityA, you will need to use another technique. Try one of these:
ActivityB sends a broadcast Intent, which ActivityA listens for
ActivityA starts ActivityB using startActivityForResult() and ActivityB sends data back to ActivityA using setResult()
Use public static (ie: global) variables to communicate
Store data in shared preferences, or a file, or a database
What you really need to understand is that, under certain conditions, the following can occur:
your app is running with ActivityA in the stack and ActivityB on the top of the stack
user presses HOME
Android moves your task to the background and eventually kills the hosting process
User returns to your app (by starting it again or selecting it from "recent task" list)
Android creates a new process for your app, and instantiates ActivityB, then calls onCreate(), onStart() and onResume() of ActivityB.
In this case, there is no instance of ActivityA anymore. That instance is dead. So ActivityB cannot communicate with ActivityA because it no longer exists.
A similar need sometimes comes up for me when there is no ActivityA around but I still need some code to be executed that ActivityB doesn't know about.
What I will usually do in this case is pass a Messenger in the intent. (A Messenger implements Parcable so it can be passed).
ActivityB will send defined messages to the messenger, And ActivityA will execute the corresponding interface calls when receiving the correct messages.
A simple solution for the problem would be making the interface Itest extend the Parcelable class. That way, the implementing class of the interface will have to implement Parcelable methods, and you can pass the interface reference in putParcelable method. Hope this helps.

Listener for activity finish from outside the class

I have a singleton class whose one of the methods accepts source activity class name and destination activity class name.
Public void handleActivityTransition(Activity srcActivity, String destActivity){
srcActivity.startActivity(new intent (srcActivity
, destActivity)) ;
}
This method basically starts the destination activity. However this method/class needs to do an operation when the destination activity is destroyed/finished (for example back button or some other action).
The way I am solving it as that I am overwriting the on destroy method of the destination activity and calling the method on the singleton class. However I feel that this is bad approach. Is there a listener or some other way to know when the destination activity is destroyed from the singleton class above.
Thanks
Seems like all you have to do is start the Intent by startActivityForResult()
and then pass the requestCode on the basis of the combination you are trying to achieve.
And then finally you can do whatever task you want to do in the onActivityResult() of the source Activity according to the request code you made earlier.
I don't really see the use of a Singleton here. Can create any combination from the source itself. But you can implement this in the singleton too if you want with some effort. Though it would be much easier to do it in the source Activity itself.

How to pass object between unrelated activates?

I need to pass an object between 2 activities that have no connection between them (meaning, neither of them calls the other).
My Main_activity extends TabActivity. I have 2 tabs : CurrencyList (extends ListActivity implements OnItemSelectedListener) and CurrencyCalculator extends Activity.
I also have class currencyData that saves data about different currencies.
In the CurrencyList activity I created a new currencyData object and initiate it with data.
How can I pass it also to the CurrencyCalculator activity?
2 quick ways:
1. use an static method in your activity to retrieve current ticket id
2. Design and implement an interface and register the fragments as listeners from the activity
Static Method is preferable for large data.
If there is absolutely no connection between them, then one method of accessing data in different Activity classes would be to declare the data members as static class members. Keep in mind that static objects from Activities persist even after you destroy the activity and Android keeps this around for some time even after the you leave the application.
These might not be the easiest ways (just some alternate approaches to the two answers given). You can use a Handler or a BroadcastReceiver to pass the data to the other activity through an intent.
Note
that your object would have to implement either Serializable or
Parcelable if you want to pass it through an intent. I have used
Serializable before and as long as your object does not have any
nested custom objects, you actually have to do no additional work.
Also the assumption is that the receiving activity is alive and is
able to receive the broadcast and/or message.
Another approach would be to write the object to a file (again it would need to be serializable) and read it back in the other activity.

what will happen is i will use Application context on dialog?

Assume i has activity A and B and external class C.
class C contains code to create dialog but requires contex.
class B is like this one
activity A is in focus(active).
Activity A call C to create dialog.
In first case, A send's it's ActivityContext to C.
In second case, C get application activity from B.
Are those two cases leads to same result? E.G dialog will pop up on a screen (on a screen of activity A)
Yes,
In android there's only 1 "Context" per application, is what documentation calls as Application Context, is where pretty much every single component related to your activity lives, so it doesn't matter what activity context reference you are using, in the end is the same since all the activities live within it, and taking on count that this app context follows the singleton design pattern be sure that is the very same instance object within the virtual machine...
Regards!

Android: Access method in an activity from another activity

My launch activity starts up another activity whose launch is set to single instance. In this 2nd activity, I have a public method. I then start up a 3rd activity and that activity needs to access the public method in the 2nd activity. I don't want to use startActivity and pass it extras because I assume the onCreate will get called (or am I wrong?) and I need to avoid the 2nd activity from reinitializing itself.
When an activity is started using startActivity, is it possible to gain access to the underlying class instance itself and simply call the method?
I actually came up with a simple solution. As a matter of fact you can access the underlying class of an activity. First, you create a class that is used to hold a public static reference to activity 2. When activity 2 is created, in its onCreate method you store "this" in the static reference. Activity 2 implements an interface with the methods that you want available to any other activity or object. The static reference you hold would be of a data type of this interface. When another activity wants to call a method in this activity, it simply accesses the public static reference and calls the method. This is no hack but is intrinsic to how Java operates and is totally legitimate.
It is not a good idea.
As I can understand method from second activity is actually not connected to particular activity while you want to call it from another one. So carry the method out to other (non-activity) class (maybe static method) and use it from both activities.
It's not directly possible to gain access to activity object started using startActivity (without using some hacks). And frankly you shouldn't even trying to accomplish this.
One Activity component can cycle through several Activity java object while its alive. For example, when user rotates the screen, old object is discarded and new activity object is created. But this is still one Activity component.
From my experience, when you need to do things you described, there is something wrong with your architecture. You either should move part of activity's responsibilities to Service or to ContentProvider, or use Intents, etc. Its hard to recommend anything more specific without knowing more details.
No there is no way to pass a reference via startActivity() however you can use some sort of shared memory to keep reference to your Activity. This is probably a bad design. However passing an extra with your Intent will not cause onCreate, that is completely related to the lifecycle.

Categories

Resources