|
| 1 | +package com.iamzain.template_android.fragments; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.net.Uri; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.support.v4.app.Fragment; |
| 7 | +import android.view.LayoutInflater; |
| 8 | +import android.view.View; |
| 9 | +import android.view.ViewGroup; |
| 10 | + |
| 11 | +import com.iamzain.template_android.R; |
| 12 | + |
| 13 | +/** |
| 14 | + * A simple {@link Fragment} subclass. |
| 15 | + * Activities that contain this fragment must implement the |
| 16 | + * {@link TestFragment.OnFragmentInteractionListener} interface |
| 17 | + * to handle interaction events. |
| 18 | + * Use the {@link TestFragment#newInstance} factory method to |
| 19 | + * create an instance of this fragment. |
| 20 | + */ |
| 21 | +public class TestFragment extends Fragment { |
| 22 | + // TODO: Rename parameter arguments, choose names that match |
| 23 | + // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER |
| 24 | + private static final String ARG_PARAM1 = "param1"; |
| 25 | + private static final String ARG_PARAM2 = "param2"; |
| 26 | + |
| 27 | + // TODO: Rename and change types of parameters |
| 28 | + private String mParam1; |
| 29 | + private String mParam2; |
| 30 | + |
| 31 | + private OnFragmentInteractionListener mListener; |
| 32 | + |
| 33 | + public TestFragment() { |
| 34 | + // Required empty public constructor |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Use this factory method to create a new instance of |
| 39 | + * this fragment using the provided parameters. |
| 40 | + * |
| 41 | + * @param param1 Parameter 1. |
| 42 | + * @param param2 Parameter 2. |
| 43 | + * @return A new instance of fragment TestFragment. |
| 44 | + */ |
| 45 | + // TODO: Rename and change types and number of parameters |
| 46 | + public static TestFragment newInstance(String param1, String param2) { |
| 47 | + TestFragment fragment = new TestFragment(); |
| 48 | + Bundle args = new Bundle(); |
| 49 | + args.putString(ARG_PARAM1, param1); |
| 50 | + args.putString(ARG_PARAM2, param2); |
| 51 | + fragment.setArguments(args); |
| 52 | + return fragment; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void onCreate(Bundle savedInstanceState) { |
| 57 | + super.onCreate(savedInstanceState); |
| 58 | + if (getArguments() != null) { |
| 59 | + mParam1 = getArguments().getString(ARG_PARAM1); |
| 60 | + mParam2 = getArguments().getString(ARG_PARAM2); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 66 | + Bundle savedInstanceState) { |
| 67 | + // Inflate the layout for this fragment |
| 68 | + return inflater.inflate(R.layout.fragment_test, container, false); |
| 69 | + } |
| 70 | + |
| 71 | + // TODO: Rename method, update argument and hook method into UI event |
| 72 | + public void onButtonPressed(Uri uri) { |
| 73 | + if (mListener != null) { |
| 74 | + mListener.onFragmentInteraction(uri); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onAttach(Context context) { |
| 80 | + super.onAttach(context); |
| 81 | + if (context instanceof OnFragmentInteractionListener) { |
| 82 | + mListener = (OnFragmentInteractionListener) context; |
| 83 | + } else { |
| 84 | + throw new RuntimeException(context.toString() |
| 85 | + + " must implement OnFragmentInteractionListener"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void onDetach() { |
| 91 | + super.onDetach(); |
| 92 | + mListener = null; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * This interface must be implemented by activities that contain this |
| 97 | + * fragment to allow an interaction in this fragment to be communicated |
| 98 | + * to the activity and potentially other fragments contained in that |
| 99 | + * activity. |
| 100 | + * <p> |
| 101 | + * See the Android Training lesson <a href= |
| 102 | + * "http://developer.android.com/training/basics/fragments/communicating.html" |
| 103 | + * >Communicating with Other Fragments</a> for more information. |
| 104 | + */ |
| 105 | + public interface OnFragmentInteractionListener { |
| 106 | + // TODO: Update argument type and name |
| 107 | + void onFragmentInteraction(Uri uri); |
| 108 | + } |
| 109 | +} |
0 commit comments