java - Android - 避免在 onResume 与 onActivityResult 中设置文本字段值的冲突

标签 java android onresume

我正在编写一个 Android 应用程序,允许用户维护产品列表。在 EnterProductData Activity 中,用户可以在表单字段中输入有关产品的信息,然后将信息保存到 SQLite DB。 EnterProductData Activity 还允许用户通过条形码扫描仪应用程序启动条形码扫描,以捕获 UPC 代码。

我面临的问题是在条形码扫描 Activity 完成并返回值后尝试在 onActivityResult() 中设置 UPC 文本字段的值。最终发生的事情是我的 onResume() 方法正在调用一个函数 (populateFields()),该函数将文本字段的值设置为当前保存在数据库中的值。这似乎是在调用 onActivityResult() 之后发生的。这意味着扫描的 UPC 被设置为文本字段值,仅在其后立即设置为空值。需要归咎的代码行旁边用星号进行注释。

我想,如果我立即在 onActivityResult() 方法中将扫描的 UPC 保存到数据库中,我可以避免这个问题,但在我看来,这似乎不是最佳实践。有人可以建议我应该做什么吗?

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;
   }
  }
 }
}

最佳答案

您可以在启动时(即在 onCreate 中)从数据库读取值并将其存储在成员变量中。需要时更新这些值。退出应用程序时将值存储回数据库。这不仅可以解决您的问题,还可以防止许多数据库交互。

关于java - Android - 避免在 onResume 与 onActivityResult 中设置文本字段值的冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4201094/

相关文章:

java - JPA--无法实例化抽象类异常

java - Android - 如何从 api.烂番茄 解析 JSONObject 和 JSONArray

android - 从我的 Android 应用程序播放 MP4 时断断续续

android - Popup 出现在 DialogFragment 后面

android - onResume 后相机表面 View 不可见

java - 如何限制 ScrollPane 内容的宽度

android - 动态创建 View 时出现问题(在 Android 中)[错误 : couldn't save which view . ..]

android - 恢复堆栈中不是最顶层的 fragment

android - 无法恢复 Activity

Java HttpURLConnection VS Android HttpURLConnection