0

I passing java.util.Date value to a fragment through arguments

From constructor of the fragment i get the correct value which i passed from previous class

but in onCreateView the java.util.Date value becomes current value

here is my code

Date mydate;
 public GraphDashboardDatedFragment(Date selectedDate) {

    this.mydate=selectedDate;
    Log.v("DateBeforePassing",selectedDate.toString());

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view= inflater.inflate(R.layout.fragment_date_dashboard_graph, container, false);
    Log.v("DateAfterPassing",mydate.toString());


    return view;
}

and here is my log file

10-12 01:06:15.220 24456-24456/? V/DateBeforePassing: Sat Oct 13 10:45:00 GMT+05:30 2018
10-12 01:06:15.240 24456-24456/? V/DateAfterPassing: Fri Oct 12 01:06:05 GMT+05:30 2018

DateAfterPassing shows always the current system time

Andreas
  • 154,647
  • 11
  • 152
  • 247

1 Answers1

0

Don't create constructor with parameters for fragment. Instead, use setArguments() after creating fragment instance. Even better solution is to use static method like newInstance(). Both ways are described in top answers in this question: How to pass a variable from Activity to Fragment, and pass it back?

thorin86
  • 604
  • 11
  • 26