Hey Guys I have this as MainActivity:
public class LoginActivity extends AppCompatActivity {
public interface LoginListener {
public void onLoginSuccess();
}
public void onLoginSuccess() {
//logged in and do a few other things
}
}
And that's my second Activity from where I want to call the method onLoginSuccess() in my MainActivity, as you can see I am doing this with an Listener...
public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {
private LoginActivity.LoginListener mListener;
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
if (mListener != null) {
mListener.onLoginSuccess();
}
else{
Toast.makeText((Activity)context, "Listener is 0", Toast.LENGTH_LONG).show();
}
}
}
MY problem is that I everytime I try it I get back: "Listener is 0" from my Toast...SO what's wrong?
Check below code for Fingerprint authentication in Android
https://gist.github.com/Evin1-/6aca8421903acca0e927eaefd85bd617
Extension of my comment above:
You need to register the mListener somehow. A pattern to do this is:
public class MyHandler {
private LoginListener mListener;
public MyHandler(LoginListener listener) {
mListener = listener;
}
// ... etc...
}
Where LoginListener is:
public interface LoginListener {
public void onLoginSuccess();
}
And your activity has:
public MyActivity implements LoginListener {
// instantiate the handler somewhere, with a reference
// to "this". "this" refers to the LoginListener interface
// which is implemented.
@Override
public void onCreate(Bundle b) {
mHandler = new MyHandler(this);
}
@Override
public void onLoginSuccess() {
Log.i(TAG, "Kewel beanZ");
}
}
Or, you can define LoginListener as an interface inside the activity if you wish, and instantiate it as:
public LoginListener mListener = new LoginListener() {
@Override
public void onLoginSuccess() {
Log.i(TAG, "Sweet sweet baby beanz");
}
};
And instead of using this, use mListener, when you create MyHandler.