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;

publicclassMyFragmentextendsFragment {

    privatestaticfinalStringFIRST_PARAM_KEY="KEY1";
    privatestaticfinalStringSECOND_PARAM_KEY="KEY2";

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

    publicstatic MyFragment newInstance(Integer param1, String param2) {
        Fragmentfragment=newMyFragment();
        Bundleargs=newBundle();
        args.putInt(FIRST_PARAM_KEY, param1);
        args.putString(SECOND_PARAM_KEY, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @OverridepublicvoidonCreate(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.

publicclassUserModelimplementsParcelable{
    int id;
    String name;
    String profilePic;

    protectedUserModel(Parcelin) {
        id = in.readInt();
        name = in.readString();
        profilePic = in.readString();
    }

    publicstatic final Creator<UserModel> CREATOR = newCreator<UserModel>() {
        @OverridepublicUserModelcreateFromParcel(Parcel in) {
            returnnewUserModel(in);
        }

        @OverridepublicUserModel[] newArray(int size) {
            returnnewUserModel[size];
        }
    };

    @Overridepublic int describeContents() {
        return0;
    }

    @OverridepublicvoidwriteToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(name);
        dest.writeString(profilePic);
    }
}

Now in UserFragement do like this :

publicclassUserFragmentextendsFragment {
    private UserModel userModel;
    private String userStr;
    publicstatic Fragment getInstance(UserModel userModel, String userStr) {
        Bundlebundle=newBundle();
        bundle.putParcelable("USER_DATA", userModel);
        bundle.putString("USER_STR", userStr);
        UserFragmentuserFragment=newUserFragment();
        userFragment.setArguments(bundle);
        return userFragment;
    }

    @OverridepublicvoidonCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments() != null){
            Bundlebundle=newBundle();
            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"