Home > database >  startActivity(new Intent()) in fragments
startActivity(new Intent()) in fragments

Time:01-24

I have created a fragment and a button on it. When this button is clicked I want the user to be signed out and directed to the login_page. I have defined signOut() like this in the ProfileFragment.java file.

    void signOut(){
        gsc.signOut().addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                getActivity().finish();
                startActivity(new Intent(fragment_profile.this,login_page.class));
                Toast.makeText(getContext(), "Signed out", Toast.LENGTH_LONG).show();
            }
        });
    }

fragment_profile is the corresponding xml file of the ProfileFragment.java file.

error is:

Cannot resolve symbol 'fragment_profile'

This method worked when I used in AppCompatActivity.

Can someone help me with correcting this? Thank you.

CodePudding user response:

Studied more on this and got this as an answer. .this does not work for fragments. Instead, getContext() works.

getActivity().finish();
startActivity(new Intent(getContext(), login_page.class));
Toast.makeText(getContext(), "Signed out", Toast.LENGTH_LONG).show();
  • Related