Learn, Share, Build

142
October 01, 2017, at 05:41 AM

I want to call an interface method implemented in activity A from a java class where web apis are written.

Flow is like moving from activity A to activity B and from activity B an api written in a separate java class is called, now on successful result of api, I want to invoke the interface method implemented in Activity A.

A -> B -> Java class -> interface() in A

I am facing problem in getting reference of activity A in java class for calling interface method. I have tried follwing code but nothing worked. Thanks for your time and help

Interface objInterface =  (Interface) context;
objInterface.funInterface();
Interface objInterface =  (Interface) context.getApplicationContext();
objInterface.funInterface();
Interface objInterface =  (Interface) new Activity_A;
objInterface.funInterface();
Answer 1

Let me describe what you're trying to do and maybe you'll see why you're having a hard time:

Interface objInterface =  (Interface) context;
objInterface.funInterface();

If this context is from Activity B, then this is wrong. Activity B and Activity A are both Contexts, but they are not the same Context.

Interface objInterface =  (Interface) context.getApplicationContext();
objInterface.funInterface();

The Application is not the same as an Activity. It cannot magically become Activity A.

Interface objInterface =  (Interface) new Activity_A;
objInterface.funInterface();

This is definitely an Activity A and it does have the implementation... but it is a different A than the one that was already existing.

Activity B shouldn't know about Activity A. Your separate java class shouldn't know about Activity B. This is a common design principle called Separation of Concerns. I suggest focus on getting data from your java class to Activity B first. Then, take that data and send it back to Activity A using the documented means of communicating between activities: https://developer.android.com/training/basics/intents/result.html

Finally, you can use a communication library like EventBus, but looking at your code it seems like you might be misunderstanding a core concept so I wouldn't recommend relying on a "magical data bus" until you understand the scope of android components. :)

READ ALSO
Learn, Share, Build

Learn, Share, Build

I am creating an Android app but have gotten to Android river with no bridge again so am here to build the bridge and cross overI am trying to implement a comment system on the Android app but it will be in from of auction / bidding system but I want to use a comment...

152
Learn, Share, Build

Learn, Share, Build

My Android Wear app keeps crashing intermittently with an illegal state exception telling me to add

195
Learn, Share, Build

Learn, Share, Build

I am an amateur in Android app developmentI tried searching a lot but couldn't get a satisfactory answer

183
Learn, Share, Build

Learn, Share, Build

I have a datepicker dialogI only want to show day and month

164