0

I am trying to track an event (OnActivityResult) from another class by creating an interface in the Activity and implementing that interface in that class. Like this:

OnAuthCompletedListener listener;

public void setOnAuthCompletedListener(OnAuthCompletedListener listener){
      this.listener = listener;  
}

public interface OnAuthCompletedListener {
     void OnAuthCompleted(Auth auth)
}

and In Another class I wrote

MainActivity activity= new MainActivity();
activity.setOnAuthCompletedListener(auth->{

});

But whenever the event occurs the app crashes with a NPE. I understand I am using different activity instance that the real one. My question is how can I use that interface in a non activity class?

I also know that there is some better solution using RXJava, but I am not smart enough to implement that, can anyone help?

Daljeet
  • 1,573
  • 2
  • 20
  • 40
Reyjohn
  • 2,654
  • 9
  • 37
  • 63
  • 1
    your "another class" should get real working `Activity` reference instead of `new` one – snachmsm Feb 12 '21 at 07:04
  • how to achieve that? – Reyjohn Feb 12 '21 at 07:05
  • well, maybe introduce new method `public void setActivityForListener(Activity act)` or just show your "another class", it would be easier to write answer – snachmsm Feb 12 '21 at 07:06
  • well, for official reason I can't show the class but It is a provider class with no reference of any context or activity in that class. – Reyjohn Feb 12 '21 at 07:09
  • well, without code its hard to guess which resolution will fit your need. but I've posted some basic way as answer, check this out – snachmsm Feb 12 '21 at 07:15

1 Answers1

1

Let's assume "another class"

public class AnotherClass implements OnAuthCompletedListener {

    // rest of code

    public void OnAuthCompleted(Auth auth){
        //interface implementation 
    }

}

Declare ArrayList<OnAuthCompleted> registeredInterfaces = new ArrayList<>(); in Activity and register its listener in array every time when you create new instance of class which implements OnAuthCompletedListener (so in this example: AnotherClass, but you can have multiple different classes implementing this interface)

AnotherClass newAnotherClass = new AnotherClass(); 
registeredInterfaces.add(newAnotherClass);

when you are nulling/destroying this newAnotherClass instance then unregister its listener

newAnotherClass.destroy();
registeredInterfaces.remove(newAnotherClass);
newAnotherClass = null;

now when OnAuthCompletedListener is called in Activity then iterate through this array and call every registered OnAuthCompletedListener.OnAuthCompleted. and don't forget to clear this array when Activity is destroyed, just to be sure that there is no memory leak

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • I have taken a MainActivity instance in my Application class and initialized it in onCreate() then in the AnotherClass I didn't take new instance of that Activity instead I have used MyApplication.getInstance().mainActivity.setOnAuthCompletedListener. Now NP Exception is not occuring and solved the problem, but is this a godd approach? – Reyjohn Feb 12 '21 at 07:22
  • well, this is very questionable... `MyApplication.getInstance()` isn't clean and reliable workaround, in some cases it may return `null`, but when you work only with `Activities` then it will work. read [THIS](https://stackoverflow.com/questions/2002288/static-way-to-get-context-in-android) post and all answers carefully incuding comments for more info – snachmsm Feb 12 '21 at 07:30