Skip to content Skip to sidebar Skip to footer

How To Pass The Data In Between Activity To Fragment Using Bundle In Android

I can store the value in one variable. Now I want pass that variable into a fragment Using the code below, I am able to load fragments: public class AndroidListFragmentActivity ex

Solution 1:

If both fragment are on other activity then can use intent

if on same activity then can do operation on that particular activity

as in this link

see onListItemClick of class TitlesFragment

**
     * Helper function to show the details of a selected item, either by
     * displaying a fragment in-place in the current UI, or starting a
     * whole new activity in which it is displayed.
     */
    voidshowDetails(int index) {
        mCurCheckPosition = index;

        if (mDualPane) {//<---------------------f on same activity then can do operation on that particular fragment// We can display everything in-place with fragments, so update// the list to highlight the selected item and show the data.
            getListView().setItemChecked(index, true);

            // Check what fragment is currently shown, replace if needed.
            DetailsFragment details = (DetailsFragment) //<------------------------see use getFragmentManager
                    getFragmentManager().findFragmentById(R.id.details);
            if (details == null || details.getShownIndex() != index) {
                // Make new fragment to show this selection.
                details = DetailsFragment.newInstance(index);

                // Execute a transaction, replacing any existing fragment// with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.details, details);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }

        } else { //<----------If both fragment are on other activity then can use intent // Otherwise we need to launch a new activity to display// the dialog fragment with selected text.
            Intent intent = new Intent();
            intent.setClass(getActivity(), DetailsActivity.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }

Solution 2:

See this response here : data sharing between fragments and activity in android

Post a Comment for "How To Pass The Data In Between Activity To Fragment Using Bundle In Android"