Home » Android » java – Android: Passing Objects Between Fragments

java – Android: Passing Objects Between Fragments

Posted by: admin June 15, 2020 Leave a comment

Questions:

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 docs:

http://developer.android.com/training/basics/fragments/communicating.html

as well as this article:

http://manishkpr.webheavens.com/android-passing-data-between-fragments/

Though all the cases mentioned above similar to what i have, it is not entirely identical. I followed a good tutorial here (Some portion of my code is based on this article):

http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

I have the following files:

RegisterActivity.java
NonSwipeableViewPager.java
ScreenSliderAdapter.java
RegisterOneFragment.java
RegisterTwoFragment.java

And the following layouts:

activity_register.xml
fragment_register_one.xml
fragment_register_two.xml

What i am trying to achieve is passing an Serializable object from RegisterFragmentOne to RegisterFragmentTwo.

So far this is what i have done (some codes are omitted):

RegisterActivity.java

public class RegisterActivity extends FragmentActivity
             implements RegisterOneFragment.OnEmailRegisteredListener{

    public static NonSwipeableViewPager viewPager;
    private ScreenSliderAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        // Initilization
        mAdapter = new ScreenSliderAdapter(getSupportFragmentManager());
        viewPager = (NonSwipeableViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(mAdapter);
    }

    public void onEmailRegistered(int position, Registration regData){
        Bundle args = new Bundle();
        args.putSerializable("regData", regData);
        viewPager.setCurrentItem(position, true);
    }
}

ScreenSliderAdapter.java

public class ScreenSliderAdapter extends FragmentPagerAdapter{

    public ScreenSliderAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            return new RegisterOneFragment();
        case 1:
            return new RegisterTwoFragment();
        case 2:
            return new RegisterThreeFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

NonSwipeableViewPager.java (extending ViewPager class, and overrides the following)

@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
    // Never allow swiping to switch between pages
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // Never allow swiping to switch between pages
    return false;
}

RegisterOneFragment.java

public class RegisterOneFragment extends Fragment {
    OnEmailRegisteredListener mCallBack;
    public interface OnEmailRegisteredListener {
        /** Called by RegisterOneFragment when an email is registered */
        public void onEmailRegistered(int position, Registration regData);
    }

public void onAttach(Activity activity){
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception.
    try {
        mCallBack = (OnEmailRegisteredListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnEmailRegisteredListener");
    }
}

... And some to execute some HTTP request via separate thread...
}

What i am trying to accomplish is that when ever a user pressed a button on RegisterOneFragment, a data will be sent to a server (and returns some validation over JSON). If the returned data is valid, the the application should go to the next fragment which is RegistrationTwoFragment.

I am having some confusion as how to pass objects between fragments, since my Fragments is created using an Adapter. And that Adapter is then attached to my Activity.

Can anyone help me with this? Thx

Edit 1:

I tried to make a shortcut (unfortunately does not work) like so:

In RegisterActivity i created:

public Registration regData;

and in RegisterOneFragment:

/* PLACED ON POST EXECUTE */
((RegisterActivity)getActivity()).regData = regData;

Finally called it in RegisterTwoFragment

Registration regData;
regData = ((RegisterActivity) getActivity()).regData;

It throws a nullPointerExceptions

Edit 2

Just to be clear, RegisterActivty contains multiple fragments. And the only way user can navigate between fragment is by clicking a button. The Activity has no Tab bar.

How to&Answers:

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.

Answer:

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());

Answer:

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();
    ...

Answer:

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);
         }