I have one Activity (1) that has two fragments. This Activity extends to another one (2) with base methods that I commonly use in my application. So I want to call a method from (2) after I click on one button that is located in one of the fragments from (1), what is the best way to do this?
I am trying to do it like this:
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isValid()){
AndroidUtils.hideKeyboard(email, getActivity());
AuthenticationActivity.login(email.getText().toString(), password.getText().toString());
}
}
});
But it says that a non-static method cannot be referenced from a static context. So I tried to do this:
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isValid()){
AndroidUtils.hideKeyboard(email, getActivity());
AuthenticationActivity instance = new AuthenticationActivity();
instance.login(email.getText().toString(), password.getText().toString());
}
}
});
But it is not working.
Try this one
((AuthenticationActivity)getActivity()).login(email.getText().toString(), password.getText().toString());
and make your login function non-static
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.