Skip to content Skip to sidebar Skip to footer

How To Pass Value From Navigationdrawerfragment.java To Mainactivity.java Then Pass To Anotherfragment Page

in my NavigationDrawerFragment.java i have this code expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildCli

Solution 1:

update your selectitem() and onNavigationDrawerItemSelected methods to accept two more arguments i.e child.id and child.name

privatevoidselectItem(int position,String id,String name) {
    mCurrentSelectedPosition = position;
    if (mDrawerListView != null) {
        mDrawerListView.setItemChecked(position, true);
    }
    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerItemSelected(position,id,name);
    }
}

pass child.id and child.name in selectItem() method so which in turn will call onNavigationDrawerItemSelected() method implemented in MainActivity.

Now in MainActivity, pass the values to the fragment using Bundle here

fragmentManager.beginTransaction()
                .replace(R.id.container, CategoryFragment.newInstance(id,name))
                .commit();

you will also need to update to CategoryFragment.newInstance(String,String) to accept the arguments

Updating code to 'setText' in 'edittext'

In your 'CategoryFragment' 'newInstance' method pass data in bundle like below

CategoryFragmentcategoryfragment=newCategoryFragment()
Bundlebundle=newBundle() 
bundle.putStringExtra("id",id)
bundle.putStringExtra("name",name)
categoryfragment.setArguements(bundle)

return categoryfragment

Now in onCreateView of Categoryfragment get this data like below

if(getArguments().getExtras != null) {
    Stringcatid= getArguments.getStringExtra("id");
    Stringcatname= getArguments.getStringExtra("name");

    yourEditText.setText(catname);
}

Hope you got my point

Post a Comment for "How To Pass Value From Navigationdrawerfragment.java To Mainactivity.java Then Pass To Anotherfragment Page"