-2

I am trying to pass variable locDistance value to the fragment. I have a tried below code inside an Activity:

protected void onPostExecute(Double distance) {
            locDistance = distance;

            Bundle bundle = new Bundle();
            bundle.putDouble("distance", locDistance );
            // Fragment class Arguments
            Fragment obj = new Fragment();
            obj.setArguments(bundle);
        }

Then getting value in fragment as follows inside onCreate():

 Double distance_value= getArguments().getDouble("distance");

But I'm getting value 0.0 . Any help would be appreciated.

  • Because it's different Fragment instance ( as in your code you does nothing with `obj` other then setting arguments) – Selvin Jul 12 '17 at 02:12
  • How can I pass value !!?? –  Jul 12 '17 at 02:24
  • 1
    You are passing it fine. You have made a **different** Fragment here. At the end of `onPostExecute`, `obj` is garbage collected and destroyed – OneCricketeer Jul 12 '17 at 02:47

2 Answers2

0

For passing value while creating new instance of a fragment, I will suggest you should use factory pattern. For more information on factory pattern just google it and you will get a lot of information. For now you could try below steps :

Create a static method in your fragment. Like this

private double locDistance = 0.0;
public static Fragment newInstance(double locDistance) {
    Fragment fragment = new Fragment();
    fragment.locDistance = locDistance;
    return fragment;
}  

public double getLocDistance(){
    return this.locDistance;
}

AND Modify your code something like this

protected void onPostExecute(Double distance) {
    locDistance = distance;
    Fragment obj = Fragment.newInstance(locDistance);
}  

Now you could retrieve locDistance value like this

Double distance = obj.getLocDistance();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rahul
  • 675
  • 6
  • 18
  • Arguments are the correct way to pass data to fragments, not fields. – OneCricketeer Jul 12 '17 at 02:46
  • It's a matter of debate. I personally feel, fields are better because Argument key are simple string not strongly typed object so you have to remember exact name and spelling of argument keys no IDE help. I'm open to suggestions – Rahul Jul 12 '17 at 03:17
  • Are static constructors considered a Factory pattern? Anyways, the fragment could be recreated upon rotation, and then the fields are nulled out, which is why arguments are preferred. https://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – OneCricketeer Jul 12 '17 at 03:21
  • That's a point but I use setRetainInstance(true); for retaining data on rotation. Is there any downside of this ? Factory pattern is one of the most used design patterns in Java. You can get a lot of information on web, by the way I'm not an expert on this. – Rahul Jul 12 '17 at 04:11
  • I learned Factory pattern in undergrad, and it's not what you're showing. This is only a static constructor. Look up yourself. It "deals with the problem of creating objects without having to specify the exact class of the object that will be created."... You are specifying the exact class within itself, thus not a Factory. Anyways, the retention of the Fragment causes memory leaks and is a workaround for an easy problem – OneCricketeer Jul 12 '17 at 05:19
0

Then getting value in fragment as follows inside onCreate():

onCreate() is never called with the code you've shown. The no-arg constructor for the Fragment is.

// Fragment class Arguments
Fragment obj = new Fragment();
obj.setArguments(bundle);

This code makes a brand new Fragment, doesn't pass anything to any existing Fragment.

If you would like to pass data back to some other class from an AsyncTask, you would could best implement that using callback listeners.

How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

Of course, you can implement the listener wherever, but by passing back to the Activity, you can then send the data into some existing Fragment rather than attempting to create a new one.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245