Android: Passing Objects Between Fragments

前端 未结 4 1889
旧巷少年郎
旧巷少年郎 2021-01-05 04:00

Before i start, i have look through question such as:

Passing data between fragments: screen overlap How to pass values between Fragments

as well as Android

相关标签:
4条回答
  • 2021-01-05 04:22

    It's easy to share objects via implementing Serializable to your custom Object. I wrote a tutorial about this here.

    From Fragment One:

    android.support.v4.app.FragmentTransaction ft = 
        getActivity().getSupportFragmentManager().beginTransaction();
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    OfficeCategoryFragment frag = new OfficeCategoryFragment();
    
    Bundle bundles = new Bundle();
    Division aDivision = divisionList.get(position);
    
    // ensure your object has not null
    if (aDivision != null) {
        bundles.putSerializable("aDivision", aDivision);
        Log.e("aDivision", "is valid");
    } else {
        Log.e("aDivision", "is null");
    }
    frag.setArguments(bundles);
    ft.replace(android.R.id.content, frag);
    ft.addToBackStack(null);
    ft.commit();
    

    In Fragment two:

    Bundle bundle = getArguments();
    Division division= (Division) bundle.getSerializable("aDivision");
    Log.e("division TEST", "" + division.getName());
    
    0 讨论(0)
  • 2021-01-05 04:23

    I would normally have setters or methods similar to this in the containing activity.

    So if I understand correctly, you want the user to access RegistrationOneFragment, then when completed, use this data, validate it, and if valid, pass it along to RegistrationTwoFragment and move the user to this Fragment.

    Could you simply call validateJson(regData) in your onEmailRegistered method to handle the validation in your activity, if it succeeds, commit a transaction to RegistrationTwoFragment.

    Then all you need are getters and setters in your activity or Fragment to say getRegistrationOneData() in the activity or setData(Registration args) in the fragment as your examples show above.

    I don't know of any way to pass the args directly into the Fragment.

    0 讨论(0)
  • 2021-01-05 04:31

    I found a solution to my question, which i am sure not the correct way to do that...

    So in RegisterActivity.java i add + modified the following lines (thx to @sturrockad):

    public Registration getRegistrationData(){
        return this.regData;
    }
    
    public void onEmailRegistered(int position, Registration regData){
        this.regData = regData;
        viewPager.setCurrentItem(position, true);
    }
    

    Then in RegisterTwoFragments.java (or in the Fragment to which i want to receive the Object):

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.fragment_register_two, container, false);
        regData = ((RegisterActivity) getActivity()).getRegistrationData();
        ...
    
    0 讨论(0)
  • 2021-01-05 04:35

    I used to set object with Pacelable or Serializable to transfer, but whenever I add other variables to object(model), I have to register it all. It's so inconvenient.

    It's super easy to transfer object between activities or fragments.

    Android DataCache

    1. put your data object to KimchiDataCache instance in your activity or fragment.

      User userItem = new User(1, "KimKevin");  // Sample Model
      
      KimchiDataCache.getInstance().put(userItem);
      
      // add your activity or fragment
      
    2. Get your data object in your activity of fragment that you added.

      public class MainFragment extends Fragment{
           private User userItem;
      
           @Override
           public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
      
               userItem = KimchiDataCache.getInstance().get(User.class);
           }
      
    0 讨论(0)
提交回复
热议问题