shubhamcool shubhamcool - 6 months ago 28
Android Question

How to sent data and text from one fragment of nav drawer to another fragment of another nav drawer

I had already work on intent say i know about intent say ,

Intent i =new Intent (first.java,next.java);
i.putExtras("flag",data);
startActivity(i);


In my app i want to send data from one fragment to another.Actually i had created navigation drawer and i want to send data from one activity of nav drawer to another activity of nav drawer but here i dont found path for it.

And i am posting my exact code here
It is my code of first fragment :-

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();

if (menuItem.getItemId() == R.id.nav_item_home) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
DayLectureFragment f = new DayLectureFragment();

fragmentTransaction.replace(R.id.containerView,f).commit();

}

if (menuItem.getItemId() == R.id.nav_item_timetable) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
DayLectureFragment f = new DayLectureFragment();

fragmentTransaction.replace(R.id.containerView,f).commit();

}

Answer

you can use these code saying :-

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            mDrawerLayout.closeDrawers();

        if (menuItem.getItemId() == R.id.nav_item_home) {
            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            DayLectureFragment f = new DayLectureFragment();
            Bundle b = new Bundle();
            b.putString("msg","monday");

            fragmentTransaction.replace(R.id.containerView,f).commit();

        }

        if (menuItem.getItemId() == R.id.nav_item_timetable) {
            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            DayLectureFragment f = new DayLectureFragment();
            Bundle b = new Bundle();
            b.putString("msg","tuesday");

            fragmentTransaction.replace(R.id.containerView,f).commit();

        }

and also you can retrive data in other fragment saying :-

      String get,msg = null;
    Bundle bundle = this.getArguments();
    if (bundle != null) {
        msg=bundle.getString("msg");
    }

    Log.d("getting bundle", String.valueOf(msg));
Comments