interface in callback andriod studio code example

Example: interface in callback andriod studio

public class CallBackFragment extends Fragment {    private CallBackListener callBackListener;    public CallBackFragment() {        // 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_call_back, container, false);    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        //getActivity() is fully created in onActivityCreated and instanceOf differentiate it between different Activities        if (getActivity() instanceof CallBackListener)            callBackListener = (CallBackListener) getActivity();    }    @Override    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {        super.onViewCreated(view, savedInstanceState);        Button btn = (Button) view.findViewById(R.id.btn_click);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(callBackListener != null)                    callBackListener.onCallBack();            }        });    }}

Tags:

Misc Example