0

I'm making AlertDialog which is used to add an item to my receipt list. In the AlertDialog there is a button which access Zxing QR Scanner. what I want is after the system get QR scan result, it intents back to the alertdialog. I already make the code in the QR Scan activity to intent back to this Activity but the problem is when it intent back, it doesn't automatically open the Alert Dialog again.

this is the code line which get result from QR Activity, what should I add here to automatically open the alertdialog?

if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if (extras == null) {
                qrResult = null;
            } else {
                qrResult = extras.getString("QRItemtype");

            }

And this is how I navigate back from the QR Scan Activity

Intent qrResult = new Intent(QRScannerActivity.this, CreateReceiptActivity.class);
                itemTypeQR = result.getContents();
                qrResult.putExtra("QRItemtype",itemTypeQR);
                startActivity(qrResult);

Here is the full code:

public class CreateReceiptActivity extends AppCompatActivity {

    @BindView(R.id.receipt_date)
    TextView date;
    @BindView(R.id.receipt_invoice)
    TextView invoiceNumber;
    @BindView(R.id.btn_receipt_add_item)
    ImageButton addItem;
    @BindView(R.id.btn_receipt_print)
    ImageButton printItem;
    @BindView(R.id.receipt_view_recycler)
    RecyclerView recyclerView;
    @BindView(R.id.create_receipt_pb_loading)
    ProgressBar pbloading;

    List<ListAutoComplete> autoCompleteList;
    ListAutoComplete listAutoComplete;


    List<ListReceiptItem> receiptItemList;
    ListReceiptItem listReceiptItem;
    ReceiptItemAdapter adapter;
    public String itemType, itemQty, itemPrice, itemDate, itemInvoice, lastInvoice, qrResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_receipt);
        ButterKnife.bind(this);


        receiptItemList = new ArrayList<>();
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ReceiptItemAdapter(this, receiptItemList);
        recyclerView.setAdapter(adapter);
        itemInvoice = invoiceNumber.getText().toString();
        itemDate = setDate(date);
        date.setText(this.getString(R.string.date, setDate(date)));

        printItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case DialogInterface.BUTTON_POSITIVE:
                                pbloading.setVisibility(View.VISIBLE);
                                cutStock();
                                break;

                            case DialogInterface.BUTTON_NEGATIVE:
                                //No button clicked
                                break;
                        }
                    }
                };

                AlertDialog.Builder builder = new AlertDialog.Builder(CreateReceiptActivity.this);
                builder.setMessage("Print Transaksi ?").setPositiveButton("Ya", dialogClickListener)
                        .setNegativeButton("Tidak", dialogClickListener).show();

            }
        });



        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if (extras == null) {
                qrResult = null;
            } else {
                qrResult = extras.getString("QRItemtype");

            }
        } else {
            qrResult = (String) savedInstanceState.getSerializable("QRItemtype");
        }
        addItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //    getTypeList();
                LayoutInflater li = CreateReceiptActivity.this.getLayoutInflater();

                final View v = li.inflate(R.layout.alertdialog_create_receipt, null);
                final AlertDialog.Builder builder = new AlertDialog.Builder(CreateReceiptActivity.this);
                builder.setView(v);
                final EditText addItemType = v.findViewById(R.id.alertdialog_receipt_type);
                final EditText addItemQty = v.findViewById(R.id.alertdialog_receipt_qty);
                final EditText addItemPrice = v.findViewById(R.id.alertdialog_receipt_price);
                Button btnSubmit = v.findViewById(R.id.alertdialog_receipt_submit);
                Button btnScan = v.findViewById(R.id.alertdialog_receipt_scanqr);
                if (qrResult == null){
                    Toast.makeText(CreateReceiptActivity.this, "Gagal scan. Masukkan Tipe Secara Manual", Toast.LENGTH_SHORT).show();
                }
                else {
                    addItemType.setText(qrResult);
                }
                btnScan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(CreateReceiptActivity.this, QRScannerActivity.class);
                        startActivity(i);

                    }
                });
                final AlertDialog alertDialog = builder.show();
                btnSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        itemType = addItemType.getText().toString().trim();
                        itemQty = addItemQty.getText().toString().trim();
                        itemPrice = addItemPrice.getText().toString().trim();
                        listReceiptItem = new ListReceiptItem(itemType, itemQty, itemPrice, "0");
                        receiptItemList.add(listReceiptItem);
                        recyclerView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        alertDialog.dismiss();
                        Toast.makeText(CreateReceiptActivity.this, "barang tertambah", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1) What does `cutStock` method do? 2) How do you navigate back to `CreateReceiptActivity` from `QRScannerActivity`? – S-Sh Mar 30 '19 at 17:11
  • @S-Sh cut stock is a method only to upload the data after you fill in the alertdialog. I navigate back using intent Intent qrResult = new Intent(QRScannerActivity.this, CreateReceiptActivity.class); itemTypeQR = result.getContents(); qrResult.putExtra("QRItemtype",itemTypeQR); startActivity(qrResult); – Justin jant Mar 30 '19 at 17:16

1 Answers1

0
  1. You can not to start new CreateReceiptActivity instance after getting QR and just return result:

In CreateReceiptActivity:

private static final int QR_REQUEST_CODE = 1;

...

  btnScan.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      Intent i = new Intent(CreateReceiptActivity.this, QRScannerActivity.class);
      startActivityForResult(i, QR_REQUEST_CODE);
    }
 });

In QRScannerActivity:

Intent qrResult = new Intent();
itemTypeQR = result.getContents();
qrResult.putExtra("QRItemtype",itemTypeQR);
setResult(RESULT_OK, intent);
finish();

Then in CreateReceiptActivity:

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data == null) {return;}
    qrResult = data.getString("QRItemtype");
    // do you want with scanned QR
  }
  1. To prevent dialog dismissing, see https://stackoverflow.com/a/7636468/8430649
S-Sh
  • 3,564
  • 3
  • 15
  • 19
  • it gives me this error java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { cmp=com.justinjunias.stockitem/.receiptactivity.CreateReceiptActivity (has extras) }} to activity {com.justinjunias.stockitem/com.justinjunias.stockitem.receiptactivity.CreateReceiptActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference – Justin jant Mar 31 '19 at 04:25
  • in this line qrResult = extras.getString("QRItemtype"); – Justin jant Mar 31 '19 at 04:26
  • @Justinjant, I have made a copy-error. Please, use ` qrResult = extras.getString("QRItemtype");` instead (updated in answer). Sorry for misleading (( – S-Sh Mar 31 '19 at 11:19