0

In Activity, i called web service and getting json response. Now i need to send this json response in one of the fragment of Activity. Please anyone help me how to send the json response to fragment without null value. Thank you in advance.

public class Gifts_Activity extends AppCompatActivity {
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.refereal_activtiy);
onSucceeded();
}
  private void onSucceeded() {
        ServiceClient serviceClient = ServiceUtil.getServiceClient();
        JsonObject json = new JsonObject();
        json.addProperty("user_id", "353");
        json.addProperty("device_id", "433");
        serviceClient.movie_quiz(json, callback);
    }

    Callback<JsonObject> callback = new Callback<JsonObject>() {
        @Override
        public void success(final JsonObject jsonObject, Response response) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                   ///here i am getting response. i need this response in fragment .

                }
            });
        }

        @Override
        public void failure(RetrofitError error) {

        }

    };
}

fragement:

public class Electronics_fragment  extends Fragment {
    Refeeral_Fragemnt.ShareAdapter adapter;
    LinearLayout lay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        if (container == null) {
            return null;
        }
        lay = (LinearLayout) inflater.inflate(R.layout.referralhistory, container, false);
        return lay;
    }
}
kartheeki j
  • 2,206
  • 5
  • 27
  • 51
  • Have you tried searching on stackoverflow? – Raghunandan Dec 17 '16 at 09:08
  • Possible duplicate of [Send data from activity to fragment in android](http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – Rafi Kamal Dec 17 '16 at 09:09
  • yes. when tried with bundle. (from search) but getting null value. – kartheeki j Dec 17 '16 at 09:09
  • post code, so that some help you – USKMobility Dec 17 '16 at 09:10
  • @RafiKamal i tried this. that answer was simple static data. My data coming from web service response. so i am getting null value in fragment – kartheeki j Dec 17 '16 at 09:10
  • @kartheekij In that case you could have a method in your Fragment (like MyFragment.myMethod()). Keep a reference of your MyFragment in your activity, and call myMethod from the activity when the json response is received. – Rafi Kamal Dec 17 '16 at 09:15

1 Answers1

0
runOnUiThread(new Runnable() {

        @Override    
        public void run() {  

            //here i am getting response. i need this response in fragment .-->

            //then here you can store(set) your json response value in SharedPreferences, please define SharedPreferences method globally

            //and  after when you call fragment retrieve(get) SharedPreferences Method in your Fragment.

        }    
    });    
Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
Dileep Patel
  • 1,988
  • 2
  • 12
  • 26