Where are the methods and actions placed in a fragment? Android Studio

0

I have a doubt, for example I have a menu where I manage three fragments, in one of them there is a simple form (To enter data), three EditText and obviously a button, but when I go to the fragment type file it is not logically Same structure as a normal Activity, so I do not know in which sector of the fragment code is placed. This is the code

private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

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

public static fr1perfil newInstance(String param1, String param2) {
    fr1perfil fragment = new fr1perfil();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

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

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

}

Obviously I do not want the code of how to make a record and those things, I just want to know the sector where they should be implemented without moving to the operation of the fragment (The code is by default, I have not implemented anything for the same ), or all the methods are done from the MainActivity where all the fragments are? . If they help me, I would appreciate a lot.

    
asked by alexander 13.07.2018 в 01:23
source

1 answer

1

In a Activity it is in onCreate() where you get references of views that are loaded in the layout.

In the case of a Fragment you can use onCreateView() to obtain the references of elements that are loaded in the layout, also here you can define the listener .

As an example, assuming that your layout loaded in Fragment contains two EditText ( edtext1 and edtext2 ) and one button ( buttonOpen ), you would get the references in this way:

private EditText editText1;
private EditText editText2;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Infla el layout para este fragment
     View view = inflater.inflate(R.layout.fragment_fr1perfil, container, false);

     //Obtiene referencias de elementos dentro del layout fragment_fr1perfil.xml
     //Usa view para obtener las referencias en el layout.
     editText1 = (EditText)view.findViewById(R.id.edtext1);
     editText2 = (EditText)view.findViewById(R.id.edtext2);

        view.findViewById(R.id.buttonOpen).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
              //Action!
           }
        });    


    return view;
}
    
answered by 13.07.2018 в 01:48