Why can’t I use Onbackpressed Listener within Ragment

Asked

Viewed 70 times

0

I have a navigation Drawer that opens several Fragments, I want to click and return, 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 to make a system with the view, but it didn’t 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;

        }
    }



}

2 answers

1

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

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

I believe this will solve your problem.

0

You can make a Systener to implement it in all Rfragments and Onbackpressed from your Activity to notify listeners.

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.