Android - avoiding conflict of setting text field values in onResume vs. onActivityResult

StackOverflow https://stackoverflow.com/questions/4201094

  •  25-09-2019
  •  | 
  •  

Question

I'm writing an Android application that allows a user to maintain a list of products. In the EnterProductData activity, the user can enter information about the product into form fields, which will then save the info to a SQLite DB. The EnterProductData activity also allows the user to initiate a barcode scan via the Barcode Scanner app in order to capture the UPC code.

The problem I'm facing is trying to set the value of the UPC text field in onActivityResult() once the barcode scan activity is complete and returns the value. What is ending up happening is my onResume() method is calling a function (populateFields()) that sets the values of the text fields to whatever is currently saved in the DB. And this seems to be happening after onActivityResult() is called. This means the scanned UPC is set as the text field value, only for an empty value to be set to it immediately after. The line of code to blame is commented with asterisks next to it.

I imagine that if I immediately save the scanned UPC to the DB in the onActivityResult() method, I can avoid this problem, but that doesn't seem to be the best practice, in my opinion. Can someone advise me on what I should do?

EnterProductData.java

public class EnterProductData extends Activity {

    private Button mScanButton;
    private EditText mUPC;
    private Button mSaveButton;
    private Long mRowId;
    private DbAdapter mDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbHelper = new DbAdapter(this);
        mDbHelper.open();

        setContentView(R.layout.enter_product_data);

        mUPC = (EditText) findViewById(R.id.UPC);
        mScanButton = (Button) findViewById(R.id.scanButton);
        mSaveButton = (Button) findViewById(R.id.saveButton);

        mRowId = (savedInstanceState == null) ? null :
        (Long) savedInstanceState.getSerializable(DbAdapter.KEY_PRODUCT_ROWID);
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong(DbAdapter.KEY_PRODUCT_ROWID): null;
        }

        populateFields();

        mScanButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                IntentIntegrator.initiateScan(EnterProductData.this);
            }
        });

        mSaveButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setResult(RESULT_OK);
                finish();
            }
        });

    }

    private void populateFields() {
        if (mRowId != null) {
            Cursor product = mDbHelper.fetchProduct(mRowId);
            startManagingCursor(product);
            mUPC.setText(product.getString(
                product.getColumnIndexOrThrow(DbAdapter.KEY_UPC))); //******
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveState();
        outState.putSerializable(DbAdapter.KEY_PRODUCT_ROWID, mRowId);
    }

    @Override
    protected void onPause() {
        super.onPause();
        saveState();
    }

    @Override
    protected void onResume() {
        super.onResume();
        populateFields();
    }

    private void saveState() {
     String upc= mUPC.getText().toString();
        if (mRowId == null) {
            long id = mDbHelper.createProduct(mRowId, UPC);
            if (id > 0) {
                mRowId = id;
            }
        } else {
            mDbHelper.updateProduct(mRowId, UPC);
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  switch(requestCode) {
   case IntentIntegrator.REQUEST_CODE: {
    if (resultCode != RESULT_CANCELED) {
     IntentResult scanResult =
     IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
     if (scanResult != null) {
      String upc = scanResult.getContents();
      mUPC.setText(upc);
     }
    }
    break;
   }
  }
 }
}
Was it helpful?

Solution

You can read the values from Database at start-up i.e., in onCreate and store in member variables. Update these values when required. Store the values back to database while exiting the application. This not only fixes your problem but also prevents many database interactions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top