Skip to content Skip to sidebar Skip to footer

Android - Pass Data From Activity To Fragment In Viewpager

I have a simple Activity with TabLayout, ViewPager and 2 Fragments in it. Here's my Activity: public class ManagementCompanyOverviewActivity extends BaseActivity implements Managem

Solution 1:

Okay, I did this.

1 step: I created public interface in my Activity and setter for it:

private OnAboutDataReceivedListener mAboutDataListener;

publicinterfaceOnAboutDataReceivedListener {
        voidonDataReceived(AboutCompanyViewModel model);
    }

publicvoidsetAboutDataListener(OnAboutDataReceivedListener listener) {
    this.mAboutDataListener = listener;
}

2 step: I implemented this interface in my Fragment and set listener:

publicclassAboutCompanyFragmentextendsBaseFragmentimplementsManagementCompanyOverviewActivity.OnAboutDataReceivedListener

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            mActivity = (ManagementCompanyOverviewActivity) getActivity();
            mActivity.setAboutDataListener(this);
        }

3 step: I overrided interface's method:

@Override
    public void onDataReceived(AboutCompanyViewModel model) {
        mPBName.setVisibility(View.INVISIBLE);
        mPBDirector.setVisibility(View.INVISIBLE);
        mPBWebsite.setVisibility(View.INVISIBLE);
        mPBEmail.setVisibility(View.INVISIBLE);
        mPBPhone.setVisibility(View.INVISIBLE);
        mPBSchedule.setVisibility(View.INVISIBLE);
        mPBAddress.setVisibility(View.INVISIBLE);

        mTVName.setVisibility(View.VISIBLE);
        mTVDirector.setVisibility(View.VISIBLE);
        mTVWebsite.setVisibility(View.VISIBLE);
        mTVEmail.setVisibility(View.VISIBLE);
        mTVPhone.setVisibility(View.VISIBLE);
        mTVSchedule.setVisibility(View.VISIBLE);
        mTVAddress.setVisibility(View.VISIBLE);

        mTVName.setText(model.getCompanyName());
        mTVDirector.setText(model.getDirectorName());
        mTVWebsite.setText(model.getWebsite());
        mTVEmail.setText(model.getEmail());
        mTVPhone.setText(model.getPhone());
        mTVSchedule.setText(model.getWorkTime());
        mTVAddress.setText(model.getAddress());
    }

That's it.

Solution 2:

Try this In activity

publicStringsendData() {
        returnYOUR_STRING;
    }

In Fragment

YOUR_ACTIVITYactivity= (YOUR_ACTIVITY) getActivity();
StringgetData= activity.SendData();

Solution 3:

You can use newInstance method in your Fragment:

publicstaticMyFragmentnewInstance(String parameter) {

        Bundle args = newBundle();
        args.putString("parameter", parameter);
        SfCategoryFragment fragment = newSfCategoryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @OverridepublicvoidonCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            parameter = getArguments().getString("parameter");
        }
    }

And in your Activity:

instead of new MyFragment() use MyFragment.newInstance("yourParam");

Solution 4:

You could also in your adapter's constructor take in the data you want to display in the fragment and set the arguments before returning a new fragment

publicclassViewPagerAdapterextendsFragmentStatePagerAdapter {
private Weather weather;

ViewPagerAdapter(FragmentManager fm, Weather weather) {
    super(fm);
    this.weather = weather;
}

@Overridepublic Fragment getItem(int position) {
    switch (position) {
        case0:
            Bundlebundle=newBundle();
            WeatherFragmentweatherFragment=newWeatherFragment();
            bundle.putParcelable("weather", weather);
            weatherFragment.setArguments(bundle);
            return weatherFragment;
        case1:
            returnnewPoemFragment();
        default:
            returnnewEmptyFragment();
    }
}

Solution 5:

From Activity or ViewPager

Bundlebundle=newBundle();
bundle.putParcelable(Keys.KEY_ITEM, cardResult);
CardItemFragmentcardItemFragment=newCardItemFragment();
cardItemFragment.setArguments(bundle);

From Fragment

@OverridepublicViewonCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     getBundle();
}


privatevoidgetBundle() {
    bundle = getArguments();
    if (null != bundle) {
        if (bundle.containsKey(Keys.KEY_ITEM)) {
            cardResult = bundle.getParcelable(Keys.KEY_ITEM);
        }
    }
}

Post a Comment for "Android - Pass Data From Activity To Fragment In Viewpager"