0

I have a fragment inside of which I call an asynctask to get some data and then update some textviews.

The problem is that the whole procedure works well when I first visit this fragment. If I change to another fragment and then come back, the textviews are not updated through settext, although if I call gettext after the update, it returns the values that were supposed to be shown.

I initialize the textviews in fragment's onCreateView and update them in asynctask's onPostExecute.

I hope I explained it well.. Any ideas what could be the issue?

onCreateView:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_medical, container, false);
        mContext = (FragmentActivity)getActivity();

        current_meas_data = (TextView) v.findViewById(R.id.current_measurement_data);

        bt = new MedicalBluetooth(mContext);
        return v;
    }

onStart:

public void onStart() {
        super.onStart();


        boolean btavailable = bt.isBluetoothAvailable();
        if (btavailable == false) {
            Toast.makeText(mContext, "Bluetooth is not available",
                   Toast.LENGTH_LONG).show();
            return;
        }

        boolean btenabled = bt.isBluetoothEnabled();
        if (btenabled) {

            bt.setupService();
            bt.startService();
        } else {

            Intent enableIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, 1);
        }

        bt.setOnDataReceivedListener(new MedicalBluetooth.OnDataReceivedListener() {
            public void onDataReceived(final byte[] data, String message) {
                new GetMeasurementTask().execute(data);
            }
        });

    }

AsyncTask:

private class GetMeasurementTask extends AsyncTask<byte[], Integer, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(mContext);


    protected void onPreExecute() {
        this.dialog.setMessage("Please wait");
        this.dialog.show();
    }


    @Override
    protected Boolean doInBackground(byte[]... params) {
        com.example.bluetoothlibrary.Measurement mes = new com.example.bluetoothlibrary.Measurement();
        mes = bt.manageData(params[0]);

        return true;


    }

    protected void onPostExecute(Boolean result) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        if (result == true) {
            //Toast.makeText(mContext, "Call Successful!", Toast.LENGTH_LONG).show();
            Log.d("BEFORE SETTEXT",current_meas_data.getText().toString());
            current_meas_data.setText(mes.data);
            Log.d("AFTER SETTEXT",current_meas_data.getText().toString());


        }
        if (result == false) {
            //Toast.makeText(mContext, "Call Error", Toast.LENGTH_LONG).show();
        }
    }
}

The log says:

BEFORE SETTEXT:

AFTER SETTEXT: 20

but the "current_meas_data" is empty.

diego10
  • 81
  • 2
  • 11

1 Answers1

0

From what I understand is that you are probably calling your Async Task in onCreatView of your Fragment A. So if you go to Fragment B and press the back button, the onCreateView of Fragment A is not triggered as it is already in the stack. For this call your AsyncTask in onResume() in Fragment A. So override onResume in Fragment A and call the asyncTask.execute() method in that.

You definitely need to post some code.

android_eng
  • 1,370
  • 3
  • 17
  • 40