1. Singleton pattern is generally not allowed to be inherited, but v7 will officially design Fragment as Singleton pattern
Use the generated Fragment that AndroidStudio comes with as follows
public class BaseFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public BaseFragment() {
// Required empty public constructor
bracket
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BaseFragment.
*/
// TODO: Rename and change types and number of parameters
public static BaseFragment newInstance(String param1, String param2{
BaseFragment fragment = new BaseFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
bracket
// ...
bracket
Moreover, it should be noted that the Fragment belongs to Singleton pattern, so is it not an official violation of the design idea? In addition, the normal design of Fragment cannot avoid inheritance. Singleton pattern is only saving resources.
2. Take a closer look at the above automatic generation method:
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BaseFragment.
*/
// TODO: Rename and change types and number of parameters
public static BaseFragment newInstance(String param1, String param2{
BaseFragment fragment = new BaseFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
bracket
Actually it is TODO, and the parameter naming is informal at first sight. I can’t believe this semi-finished product is officially provided.
If you don’t understand, you don’t know where the official propaganda is first.
Fragment
Do you want to use Singleton pattern?
In addition, automatically generatedFragment
It is for you to realize it yourself, not for you to use it directly, thoseTODO
Is what you want to do, how do you take him as the official write. If you have achieved the function in your life, why do you need to write the code?