Best practice for instantiating a new Android Fragment

前端 未结 13 1513
暖寄归人
暖寄归人 2020-11-21 04:38

I have seen two general practices to instantiate a new Fragment in an application:

Fragment newFragment = new MyFragment();

and

<         


        
相关标签:
13条回答
  • 2020-11-21 04:59

    Best practice to instance fragments with arguments in android is to have static factory method in your fragment.

    public static MyFragment newInstance(String name, int age) {
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        bundle.putInt("age", age);
    
        MyFragment fragment = new MyFragment();
        fragment.setArguments(bundle);
    
        return fragment;
    }
    

    You should avoid setting your fields with the instance of a fragment. Because whenever android system recreate your fragment, if it feels that the system needs more memory, than it will recreate your fragment by using constructor with no arguments.

    You can find more info about best practice to instantiate fragments with arguments here.

    0 讨论(0)
  • 2020-11-21 05:01

    Since the questions about best practice, I would add, that very often good idea to use hybrid approach for creating fragment when working with some REST web services

    We can't pass complex objects, for example some User model, for case of displaying user fragment

    But what we can do, is to check in onCreate that user!=null and if not - then bring him from data layer, otherwise - use existing.

    This way we gain both ability to recreate by userId in case of fragment recreation by Android and snappiness for user actions, as well as ability to create fragments by holding to object itself or only it's id

    Something likes this:

    public class UserFragment extends Fragment {
        public final static String USER_ID="user_id";
        private User user;
        private long userId;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            userId = getArguments().getLong(USER_ID);
            if(user==null){
                //
                // Recreating here user from user id(i.e requesting from your data model,
                // which could be services, direct request to rest, or data layer sitting
                // on application model
                //
                 user = bringUser();
            }
        }
    
        public static UserFragment newInstance(User user, long user_id){
            UserFragment userFragment = new UserFragment();
            Bundle args = new Bundle();
            args.putLong(USER_ID,user_id);
            if(user!=null){
                userFragment.user=user;
            }
            userFragment.setArguments(args);
            return userFragment;
    
        }
    
        public static UserFragment newInstance(long user_id){
            return newInstance(null,user_id);
        }
    
        public static UserFragment newInstance(User user){
            return newInstance(user,user.id);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:02

    I'm lately here. But somethings I just known that might help you a bit.

    If you are using Java, there is nothing much to change. But for kotlin developers, here is some following snippet I think that can make you a basement to run on:

    • Parent fragment:
    inline fun <reified T : SampleFragment> newInstance(text: String): T {
        return T::class.java.newInstance().apply {
            arguments = Bundle().also { it.putString("key_text_arg", text) }
        }
    }
    
    • Normal call
    val f: SampleFragment = SampleFragment.newInstance("ABC")
    // or val f = SampleFragment.newInstance<SampleFragment>("ABC")
    
    • You can extend the parent init operation in child fragment class by:
    fun newInstance(): ChildSampleFragment {
        val child = UserProfileFragment.newInstance<ChildSampleFragment>("XYZ")
        // Do anything with the current initialized args bundle here
        // with child.arguments = ....
        return child
    }
    
    

    Happy coding.

    0 讨论(0)
  • 2020-11-21 05:04

    Some kotlin code:

    companion object {
        fun newInstance(first: String, second: String) : SampleFragment {
            return SampleFragment().apply {
                arguments = Bundle().apply {
                    putString("firstString", first)
                    putString("secondString", second)
                }
            }
        }
    }
    

    And you can get arguments with this:

    val first: String by lazy { arguments?.getString("firstString") ?: "default"}
    val second: String by lazy { arguments?.getString("secondString") ?: "default"}
    
    0 讨论(0)
  • 2020-11-21 05:05

    setArguments() is useless. It only brings a mess.

    public class MyFragment extends Fragment {
    
        public String mTitle;
        public String mInitialTitle;
    
        public static MyFragment newInstance(String param1) {
            MyFragment f = new MyFragment();
            f.mInitialTitle = param1;
            f.mTitle = param1;
            return f;
        }
    
        @Override
        public void onSaveInstanceState(Bundle state) {
            state.putString("mInitialTitle", mInitialTitle);
            state.putString("mTitle", mTitle);
            super.onSaveInstanceState(state);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
            if (state != null) {
                mInitialTitle = state.getString("mInitialTitle");
                mTitle = state.getString("mTitle");
            } 
            ...
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:07

    If Android decides to recreate your Fragment later, it's going to call the no-argument constructor of your fragment. So overloading the constructor is not a solution.

    With that being said, the way to pass stuff to your Fragment so that they are available after a Fragment is recreated by Android is to pass a bundle to the setArguments method.

    So, for example, if we wanted to pass an integer to the fragment we would use something like:

    public static MyFragment newInstance(int someInt) {
        MyFragment myFragment = new MyFragment();
    
        Bundle args = new Bundle();
        args.putInt("someInt", someInt);
        myFragment.setArguments(args);
    
        return myFragment;
    }
    

    And later in the Fragment onCreate() you can access that integer by using:

    getArguments().getInt("someInt", 0);
    

    This Bundle will be available even if the Fragment is somehow recreated by Android.

    Also note: setArguments can only be called before the Fragment is attached to the Activity.

    This approach is also documented in the android developer reference: https://developer.android.com/reference/android/app/Fragment.html

    0 讨论(0)
提交回复
热议问题