Skip to content Skip to sidebar Skip to footer

Pass String Between Two Fragments Without Using An Activity

I need to pass the string curDate between NewDateFragment and NewEventFrament, i see many peoples using Bundle, but using these i still having NullPointerException. I transform the

Solution 1:

An alternative to the given answer would be to use Events. If you really want to avoid coupling your code - meaning getting rid of unnecessary dependence between classes, having a variable inside your Activity is not a good idea. Here is my suggestion:

  • Add EventBus library to your Gradle file:

    compile'de.greenrobot:eventbus:2.4.0'
  • Create a simple plain old java class to represent your event:

    publicclassCalendarDateSelectedEvent{
       privateString currentDate;
    
       publicCalendarDateSelectedEvent(String date){
    
          this.currentDate = date;
       }
    
       publicStringgetCurrentDate(){
    
          return currentDate;
       }
    }
    
  • Inside your first fragment where a date is picked, you can post an event to your second fragment as soon as the date is selected like this:

    //somewhere when  a date is selectedonSelectedDayChanged(String dateSelected){
       EventBus.getDefault().post(newCalendarDateSelectedEvent(dateSelected));
    }
    
  • Finally, inside your second fragment, do the following:

    //could be inside onCreate(Bundle savedInstanceState) method@OverridepublicvoidonCreate(Bundle saveInstanceState){
       //......EventBus.getDefault().register(this);
    }
    
    @OverridepublicvoidonDestroy(){
       super.onDestroy();
       EventBus.getDefault().unregister(this);
    }
    
    //very important piece here to complete the jobpublicvoidonEvent(CalenderDateSelectedEvent event){
    
        String currentDate = event.getCurrentDate();
        //you can now set this date to view.
    }
    

At this point, you might be asking, why all the hussle to have all these code; but the answer is simple: the activity doesn't have to really know what is happening in either fragments. You have eliminated unnecessary coupling in your code.

If you ever change the activity to do something else, you won't have to change the fragment code.

I hope this helps you see the difference between the two approaches to communicating between fragments!

The first approach (the answer you accepted, involves 3 parties while the second approach involves only 2 parties). It is up to you to choose.

Enjoy!

Solution 2:

If they're fragments in the same activity, you could very easily access data between them using the activity.

Step 1: Declare a String curDate in your Activity:

publicString curDate; //you could also make it private and add a public getter & setter

Step 2: In your NewDateFragment, inside the onSelectedDayChange(), set the activity's curDate to the current date you just calculated:

getActivity().curDate = curDate;

Step 3: In your NewEventFragment, simply get the value from the activity:

publicStringcurDate= getActivity().curDate;

Post a Comment for "Pass String Between Two Fragments Without Using An Activity"