1

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 extends Activity {
    Fragment2 f2;
    public static String itemname;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.apetiserfragement);
        itemname=getIntent().getStringExtra("itemname");
        Bundle args=new Bundle();
        args.putString("itemname", itemname);
        f2=new Fragment2();
        f2.setArguments(args);
    }
} /* (Here I load fragment using xml page) itemname */

The output is splitted into 2 windows one for extend for listfragment (for listview) and one for fragments.

Fragment2.xml

public class Fragment2 extends Fragment {
    String itemname;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        System.out.println(getArguments().getString("itemname"));

        return inflater.inflate(R.layout.fragment2, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
}

AndroidListFragmentActivity in this class itemname I want pass Fragment2.class.. please help me

evotopid
  • 5,288
  • 2
  • 26
  • 41
naveen
  • 721
  • 3
  • 11
  • 16
  • See this response here : [data sharing beetween fragments and activity in androidi][1] [1]: http://stackoverflow.com/questions/13445594/data-sharing-between-fragments-and-activity-in-android/20521851#20521851 – Alain Beauvois Dec 11 '13 at 16:08

2 Answers2

3

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.
     */
    void showDetails(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);
        }
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

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

Community
  • 1
  • 1
Alain Beauvois
  • 5,896
  • 3
  • 44
  • 26