Implement fragment method

1

I stopped programming long ago and well, now that I am developing I am in a mess. What happens is that I have an application with navigation drawer and that calls fragments. Now, I followed a tutorial to login with firebase and I want the logout option to be in a fragment, the settings snippet, but I can not make it work. I have a logout method in mainActivity and I can not call it from the snippet, how could I make it work. Attachment method:

public void logOut(View view){
    firebaseAuth.signOut();

    Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            if(status.isSuccess()){
                goLogInScreen();
            }else {
                Toast.makeText(getApplicationContext(),"No se pudo cerrar sesión",Toast.LENGTH_SHORT).show();
            }
        }
    });
}

and the fragment:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    btnLogout = (Button) getView().findViewById(R.id.btnLogout);

    btnLogout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MainActivity main = new MainActivity();
            main.logOut(v);
        }
    });
}
    
asked by Augusto Mauricio B 30.11.2017 в 20:20
source

1 answer

-1

Ideally, you do it with interfaces, but for now you can do it like this:

 btnLogout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((MainActivity)getActivity()).logOut(v);
        }
    });
    
answered by 30.11.2017 / 20:39
source