0

I want to pass a data from activity to fragment. The activity which i have created is navigation drawer activity. Once i have click on menu, it will direct to the fragment page. would like to ask how to pass different data to the same fragment?

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    Fragment fragment = null;
    Class fragmentClass = null;
    Bundle bundle = new Bundle();
    if (id == R.id.frag_one) {
        fragmentClass = Fragment1.class;
    } else if (id == R.id.frag_two) {
        fragmentClass = Fragment2.class;
    } else if (id == R.id.frag_three) {
        fragmentClass = Fragment3.class;
    } else if (id == R.id.frag_four) {
        fragmentClass = Fragment4.class;
    }
    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flContent,       fragment).commit();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
} 
michelletbs
  • 809
  • 2
  • 9
  • 19
  • Save it to ``SharedPreferences`` , ``Bundle`` just work when you sent intent – Danh DC Jan 19 '16 at 07:58
  • Possible duplicate of [Send data from activity to fragment in android](http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – Dhaval Parmar Jan 19 '16 at 07:58
  • @DhawalSodhaParmar, I gt error "java.lang.NullPointerException" when i send data using intent.. – michelletbs Jan 19 '16 at 08:02
  • put logs in question with appropriate code. there is no need of intent when send data from activity to fragment when fragment is child of that activity(in your case navigation drawer). – Dhaval Parmar Jan 19 '16 at 08:02
  • @DhawalSodhaParmar, by creating a new instance with parameter works for me. – michelletbs Jan 20 '16 at 01:10
  • @DanhDC, i have found the solution, by creating new instance with parameter , we are able to pass the value from activity to fragment – michelletbs Jan 20 '16 at 01:11
  • @michelletbs: its not a good idea to create new instance every time. – Dhaval Parmar Jan 20 '16 at 04:04

1 Answers1

1
Try like this
  AdapterClass:
  @Override
  public Fragment getItem(int index) {
try
{
    switch (index) {
    case 0:
        return GamesFragment.newInstance(0);
    case 1:
        return GamesFragment.newInstance(1);
    case 2:
        return GamesFragment.newInstance(2);
    case 3:
        return GamesFragment.newInstance(3);
    case 4:
        return GamesFragment.newInstance(4);
    case 5:
        return GamesFragment.newInstance(5);
    case 6:
        return GamesFragment.newInstance(6);
    case 7:
        return GamesFragment.newInstance(7);
    case 8:
        return GamesFragment.newInstance(8);
    case 9:
        return GamesFragment.newInstance(9);
    }
 }catch(Exception e)
{
e.printStackTrace();
}

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 10;
}

  Activity:
 public static GamesFragment newInstance(int page) {
    GamesFragment fragmentFirst = new GamesFragment();
    Bundle args = new Bundle();
    args.putInt("someInt", page);
    fragmentFirst.setArguments(args);
    return fragmentFirst;
}   
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49