0

I want to know if I can save a block of code that I have written in all fragments of the app.

The code in the fragment Nro 1 is this:

public class Fragment1 extends Fragment {

@InjectView(R.id.txtRP) EditText txtRP;

public static Fragment1 newInstance() {
    Fragment1 fragment = new Fragment1();
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment1, container, false);
    ButterKnife.inject(this, rootView);
    return rootView;
}

@OnEditorAction(R.id.txtRP)
boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if(actionId == EditorInfo.IME_ACTION_NEXT){
        return true;
    }
    if ((event == null && (actionId == EditorInfo.IME_ACTION_DONE)) || (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)){
        if (event == null || event.getAction() == KeyEvent.ACTION_DOWN){
            buscaRP();
            return true;
        }
    }
    return true;
}

private void buscaRP(){
    Toast.makeText(getActivity(), "Button in fragment 1.", Toast.LENGTH_SHORT).show();
}

@Override 
public void onDestroyView() {
    super.onDestroyView();
    ButterKnife.reset(this);
}

}

As you can see, the EditText called "txtRP", in the event onEditorAction call the function "buscaRP()".

In the other 3 fragments is the same, so...

How I can save that block of code and not have to declare the event onEditorAction in all fragments?. Can I create the event onEditorAction in a separate class and call it from there?

Thanks in advance !!!

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
seba123neo
  • 4,688
  • 10
  • 35
  • 53

0 Answers0