Why can not I use OnbackPressed Listener within fragment

0

I have a navigation drawer that opens several fragments, I want to click and go back, check the visibility of a layout and do something with it, and instead of closing the app, but I can not override the backpressed inside the fragment, I tried make a listener with the view, but it did not work.

    package doupenglish.com.br.doup.Fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import doupenglish.com.br.doup.MainActivity;
import doupenglish.com.br.doup.R;
import mehdi.sakout.fancybuttons.FancyButton;


public class PreparacaoFragment extends Fragment implements View.OnClickListener{

    private ConstraintLayout layoutunit,layoutdia;
    private FancyButton btnunit1,btnunit2,btndia1;

    public PreparacaoFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_preparacao, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        layoutunit = view.findViewById(R.id.layoutunit);
        layoutdia = view.findViewById(R.id.layoutdia);
        btnunit1 = view.findViewById(R.id.btnunit1);
        btnunit1.setOnClickListener(this);
        btndia1 = view.findViewById(R.id.btndia1);
        btndia1.setOnClickListener(this);

        view.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keycode, KeyEvent keyEvent) {
                if (keycode == KeyEvent.KEYCODE_BACK){
                    if(layoutunit.getVisibility() == View.GONE){
                        layoutunit.setVisibility(View.VISIBLE);
                        layoutdia.setVisibility(View.GONE);
                    }
                }
                return false;
            }
        });

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btnunit1:
                layoutunit.setVisibility(View.GONE);
                layoutdia.setVisibility(View.VISIBLE);
                break;
            case R.id.btnunit2:

                break;
            case R.id.btndia1:
                Toast.makeText(getContext(), "Layout Exercicios", Toast.LENGTH_SHORT).show();
                break;

        }
    }



}
    
asked by anonymous 08.12.2017 / 12:37

2 answers

1

You can create a listener for this, something that will take a good handful of code, or you can use onDetach in your Fragment to do this.

@Override
public void onDetach() {
    super.onDetach();
    SEU CÓDIGO AQUI
}

I think this will solve your problem.

    
08.12.2017 / 17:03
0

You can make a listener implement it in all fragments and OnBackPressed of your Activity notify the listeners.

I hope I have helped!

    
11.12.2017 / 14:04