By clicking on fragment pulse the activity

1

I have the problem that when creating all my fragments the buttons of the activity continue to work, that is, to be in one of the fragments if I give somewhere on the screen where there is a button in the activity this is pressed.

the java file of the fragment I leave empty and I do everything from the activity , can that be the reason?

public class LogoutFragment extends Fragment {

public LogoutFragment() { }

    final View vista = inflater.inflate( R.layout.fragment_logout, container, false );

     logoutNo = vista.findViewById( R.id.imageButton3 );
     logoutYes = vista.findViewById( R.id.imageButton2 );

    logoutYes.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //FirebaseAuth.getInstance().signOut();
            // auth.signOut();
            AuthUI.getInstance()
                    .signOut( getActivity() )
                    .addOnCompleteListener( new OnCompleteListener<Void>() {
                        public void onComplete(@NonNull Task<Void> task) {
                            // ...
                        }
                    } );

            Toast.makeText( getActivity(), "Te has deslogueado", Toast.LENGTH_SHORT ).show();

            Intent intent = new Intent(getActivity(), MainActivity.class);
            getActivity().startActivity(intent);

        }
    } );

    logoutNo.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getFragmentManager().beginTransaction().
                    remove( getFragmentManager().findFragmentById( R.id.contenedor ) ).commit(); //  codigo para salir del fragment hacia la activity
        }
    } );

    return vista;

in the main to choose the fragments I have this, I do not know if it can come around

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    FragmentManager fragmentManager = getFragmentManager();

    if (id == R.id.nav_camera) {
        Toast.makeText( context, R.string.very_soon, Toast.LENGTH_SHORT ).show();
    } else if (id == R.id.nav_gallery) {
        fragmentManager.beginTransaction().replace( R.id.contenedor, new GalleryFragment() ).commitAllowingStateLoss();
    } else if (id == R.id.nav_slideshow) {
        Toast.makeText( context, R.string.very_soon, Toast.LENGTH_SHORT ).show();

    }  else if (id == R.id.nav_logout) {
        fragmentManager.beginTransaction().replace( R.id.contenedor, new LogoutFragment() ).commitAllowingStateLoss();

    }

    DrawerLayout drawer = findViewById( R.id.drawer_layout );
    drawer.closeDrawer( GravityCompat.START );
    return true;
}
    
asked by lujan 19.07.2018 в 23:32
source

1 answer

0

The "problem" you mention is precisely what you intuit, you actually have the listener buttons in the activity and the fragment is contained within the Activity.

You must change the logic so that the buttons only activate when clicking on fragment .

The fragment_logout.xml layout that loads your Fragment must have these buttons and in the same fragment you must obtain its references and assign listeners to perform the click action.

This is an example of how to obtain the reference of a button (it must be contained in the layout) and assign its OnClickListener :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   //Infla layout que contiene botones.
   View view = inflater.inflate( R.layout.fragment_logout, container, false );
    //Obtiene referencias.
    Button mybutton = (Button) view.findViewById(R.id.btnAgregar);
    mybutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            //Define acción de botón.

            }
        });


    return view
}
    
answered by 20.07.2018 в 02:33