Jay Patel
Jay Patel

Reputation: 3

Sending Bundle from Main Activity to Fragment

I've followed the google tutorial and am not getting any errors in my code, but for some reason the data in my bundle does not seem to be being sent from my Main Activity to my Fragment (my app just keeps returning "0.00"). Please help.

MAIN ACTIVITY:

@Override
public void assignButtonBWHW1Clicked(String assignButtonBWHWClicked) {

    if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {

        assignFragment1 = new assignFragment1();
        buttonsGeneric = new genericBackNextFragment();

        splitItFragmentManager3 = getFragmentManager();
        splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
        splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);

        splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
        splitItFragmentTransaction3.addToBackStack(null);

        splitItFragmentTransaction3.commit();

        Bundle bundleAssign = new Bundle();
        bundleAssign.putDouble("price1", 22);
        assignFragment1.setArguments(bundleAssign);

    }
}

FRAGMENT:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    assignFragment1View = inflater.inflate(R.layout.assign_fragment_1, container, false);

    item1 = (TextView) assignFragment1View.findViewById(R.id.assign1_food_item_1);
    item2 = (TextView) assignFragment1View.findViewById(R.id.assign1_food_item_2);
    priceItem1 = (TextView) assignFragment1View.findViewById(R.id.assign1_price_item_1);
    priceItem2 = (TextView) assignFragment1View.findViewById(R.id.assign1_price_item_2);

    Bundle bundleAssign = this.getArguments();
    Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);
    priceItem1.setText(String.valueOf(price1Double));

    return assignFragment1View;
}

Upvotes: 0

Views: 298

Answers (4)

Satyen Udeshi
Satyen Udeshi

Reputation: 3243

just update your key in the fragment side from priceItem1 to price1.

Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);

to

Double price1Double = bundleAssign.getDouble("price1", 0.00);

To make things clear.this happens because you are using seperate keys while adding data to bundle and retrieving using a different key. so it is returning default value i.e 0.00 for that priceItem1 key

Upvotes: 1

Shadow Droid
Shadow Droid

Reputation: 1696

Two change you will have to do it in your code.
first one is set the argument before transaction

if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {

        assignFragment1 = new assignFragment1();
        Bundle bundleAssign = new Bundle();
        bundleAssign.putDouble("price1", 22);
        assignFragment1.setArguments(bundleAssign);
        buttonsGeneric = new genericBackNextFragment();

        splitItFragmentManager3 = getFragmentManager();
        splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
        splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);

        splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
        splitItFragmentTransaction3.addToBackStack(null);

        splitItFragmentTransaction3.commit();



    }

and second one always use same key to set and retrieve data within your bundle so change in fragment

  Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);

to

  Double price1Double = bundleAssign.getDouble("price1", 0.00);

Upvotes: 1

Valentin Baryshev
Valentin Baryshev

Reputation: 2205

Try to place bundle initialisation to fragment before you get fragment nmanager.

if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {

    assignFragment1 = new assignFragment1();
    buttonsGeneric = new genericBackNextFragment();

    Bundle bundleAssign = new Bundle();
    bundleAssign.putDouble("price1", 22);
    assignFragment1.setArguments(bundleAssign);

    splitItFragmentManager3 = getFragmentManager();
    splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
    splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);

    splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
    splitItFragmentTransaction3.addToBackStack(null);

    splitItFragmentTransaction3.commit();

}

Upvotes: 0

Mohammad Tauqir
Mohammad Tauqir

Reputation: 1815

Try

Bundle bundleAssign = getArguments();

Or,
Bundle bundleAssign = getActivity().getArguments()

Instead of

Bundle bundleAssign = this.getArguments();

Get Value like

double result = bundleAssign.getDouble("price1");

Upvotes: 0

Related Questions