简体   繁体   中英

TextView not setText on onActivityResult Fragment

This is my code to scan barcode and set content to TextView :

public class scanCodeFragment extends android.support.v4.app.Fragment implements View.OnClickListener{

  Button btnScan;
  TextView contentTxt;
  String substrCode, scanContent;
  View v;

  public scanCodeFragment(){
  }

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

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    v = inflater.inflate(R.layout.fragment_scan_code, container, false);
    btnScan = (Button) v.findViewById(R.id.btnScan);
    contentTxt = (TextView) v.findViewById(R.id.scan_content);

    btnScan.setOnClickListener(this);

    return v;
  }

  public void onClick(View v){
    if (v.getId() == R.id.btnScan){
        IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
        scanIntegrator.initiateScan();
    }
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanningResult != null) {
        scanContent = scanningResult.getContents();
        substrCode = scanContent.substring(2, 10);
        contentTxt.setText("NIS: " + substrCode);
    }
    else {
        Toast toast = Toast.makeText(getActivity(), "Kode barcode salah", Toast.LENGTH_SHORT);
        toast.show();
    }
  }
}

In onActivityResult method , I can't get the result after scan the barcode code. Before this code fragment, I can get the result from onActivityResult.

i don't know what your initiateScan() so keep it if you need it

final int requestCode = 100;

public void onClick(View v){
    if (v.getId() == R.id.btnScan){
      IntentIntegrator intent = new IntentIntegrator(getContext(), MainActivity.class);
            intent.putExtra(RECIPE_EXTRA, extraObject);
            startActivityForResult(intent, requestCode);
    }
}

From the code snippet you have provided, I suggest you to create a separate Activity for scanning, instead of a Class file, in your case which is IntentIntegrator , and start it when your Scan Button is clicked.

So, create IntentIntegratorActivity and start it like this.

public void onClick(View v){
    if (v.getId() == R.id.btnScan){
        IntentIntegratorActivity intent = new Intent(getActivity(), IntentIntegratorActivity.class);
        startActivityForResult(intent, SCAN_INTENT_CODE);//Remember this code, which is an Integer value
    }
}

Then, in OnCreate method of IntentIntegratorActivity , do the scanning and return the scan data via an Intent .

Intent returnIntent = new Intent();
returnIntent.putExtra("SCAN_DATA_KEY", scanData);//Send scan data from here and remember the key SCAN_DATA_KEY
setResult(RESULT_OK, returnIntent);
finish();

Then this data should be caught inside onActivityResult method in the Fragment Class .

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case ConstList.SCAN_INTENT_CODE:
        String scanData = data.getStringExtra("SCAN_DATA_KEY");//Use this key to get the data. 
        //I have assumed that the data is String
        contentTxt.setText("NIS: " + scanData);//Set data to the Text field
        break;     
    }
}

Try to change this line IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity()); to this IntentIntegrator scanIntegrator = new IntentIntegrator(this); .

The reason is that if you pass an Activity as parameter inside IntentIntegrator then on initiateScan will be called activity.startActivityForResult(intent, code) - so result won't be passed to fragment (try to check onActivityResult for parent Activity, it will be called). But if you pass Fragment as parameter inside IntentIntegrator - will be called fragment.startActivityForResult(intent, code) , so Android framework will be able to call onActivityResult inside your fragment for result.

OnActivityResult() called only if you call startActivityForResult()

I call the startActivityForResult() in Fragment and implement the onActivityResult in Fragment, the onActivityResult() will be called correctly.

you can not call startActivityForResult() in activity, otherwise the onActivityResult() in Fragment will not be called

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM