HeisenBerg February 2016

update textviews on different fragments based on asyntask result

I've an activity which on create calls an async task.

public class Sync extends BaseActivity implements OnClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sync);
        CheckForDeviceTask(Sync.this).execute(Constants.DEVICE_ADDRESS);
    }
}

Now the result of the async task determines which fragment to load inside the activity which I was able to do. But could not figure how to update the textviews inside the fragments. Please take a look at the onPostExecute() method. After loading a fragment how do I update the TextViews based on the result?

public class CheckForDeviceTask extends AsyncTask<String, Integer, String> {
    private static final String TAG = "CheckForDeviceTask";
    public static final int CONNECTED_DEVICE_EXISTS = 1;
    public static final int WAITING_TEMP_PASSWORD = 2;
    private Activity activity;

    // private DialogFragment dialogFragment;

    public CheckForDeviceTask(Activity activity) {
        super();
        this.activity = activity;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // FragmentManager fm = activity.getFragmentManager();
        // dialogFragment = ProgressDialogFragment.newInstance("Saving");
        // dialogFragment.show(fm, "dialog");
    }

    @Override
    protected String doInBackground(String... params) {
        if (GCHUtils.isConnected(activity)) {
            return GCHUtils.httpGet(RestApiPaths.CHECK_FOR_DEVICE
                    + params[0].toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // dialogFragment.dismiss();
        try {
            if (result != null && !result.isEmpty()) {
                Map<String, Object> resultMap = MapUtility.jsonToMap        

Answers


App Crazy February 2016

First put the data you get eg email and name, in a bundle and send the bundle as arguments to the destination Fragment,as shown below:-

          `Bundle bundle = new Bundle();
            bundle.putSerializable("email ", email );
            bundle.putBoolean("name", name);
            fragment.setArguments(bundle);` 

In the destination Fragment get the data as follows:

                  `String email=getArguments().getString("email");
                   String name=getArguments().getString("name");`

Post Status

Asked in February 2016
Viewed 2,819 times
Voted 13
Answered 1 times

Search




Leave an answer