Skip to content Skip to sidebar Skip to footer

Correct Approach For Providing Argument To A Fragment

I have a class like this: public Class Row { private int data = 0; private View.OnClickListener callback = null; public void setData(int a) { data = a; } public vo

Solution 1:

Better approach in my knowledge is that you make Row serializeable and set instance of Row in bundle and set bundle as argument of desired Fragment, after recreation you can get that bundle in onResume() method using function getArguments(); so that your data will persist.


Solution 2:

Best approach in my opinion is bellow, in this example i have used Dart liblary for less code in your fragment;

public class MyFragment extends Fragment {

    private static final String FIRST_PARAM_KEY = "KEY1";
    private static final String SECOND_PARAM_KEY = "KEY2";

    @InjectExtra(FIRST_PARAM_KEY)
    Integer firstParam;
    @InjectExtra(SECOND_PARAM_KEY)
    String SecondParam;

    public static MyFragment newInstance(Integer param1, String param2) {
        Fragment fragment = new MyFragment();
        Bundle args = new Bundle();
        args.putInt(FIRST_PARAM_KEY, param1);
        args.putString(SECOND_PARAM_KEY, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Dart.inject(this, getArguments());
    }
}

Whenever you want to create new fragment, use: Fragment fragment = MyFragment.newInstance(123, "text"). Remember if you want to pass object, that object have to implements Parcelable.


Solution 3:

The best approach is put your data in bundle and setArgument in fragment. Here is an example - Let say I have a Object of class UserModel to pass to UserFragment.

public class UserModel  implements Parcelable{
    int id;
    String name;
    String profilePic;

    protected UserModel(Parcel in) {
        id = in.readInt();
        name = in.readString();
        profilePic = in.readString();
    }

    public static final Creator<UserModel> CREATOR = new Creator<UserModel>() {
        @Override
        public UserModel createFromParcel(Parcel in) {
            return new UserModel(in);
        }

        @Override
        public UserModel[] newArray(int size) {
            return new UserModel[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(name);
        dest.writeString(profilePic);
    }
}

Now in UserFragement do like this :

public class UserFragment extends Fragment {
    private UserModel userModel;
    private String userStr;
    public static Fragment getInstance(UserModel userModel, String userStr) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("USER_DATA", userModel);
        bundle.putString("USER_STR", userStr);
        UserFragment userFragment = new UserFragment();
        userFragment.setArguments(bundle);
        return userFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments() != null){
            Bundle bundle = new Bundle();
            userModel = bundle.getParcelable("USER_DATA");
            userStr = bundle.getString("USER_STR");
        }
    }
}

Now use newInstance method to create a new object of the fragment.

Hope it will help you :)


Post a Comment for "Correct Approach For Providing Argument To A Fragment"