OnActivityResult Intent data is incorrect

This is the first time I enter startActivityForResult and I am having a problem.

Activity A (ActivityMyList) starts Activity B (ActivityQuickList), waiting for the result:

Intent intentLaunchQuickList = new Intent(ActivityMyList.this, ActivityQuickList.class);
startActivityForResult(intentLaunchQuickList, REQUEST_QUICKLIST);

When a user clicks on an Activity B list item, he returns "ql_id" in Activity A:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    QuickListItem qlItem = m_Adapter.getItem(position);
    if (qlItem != null && qlItem.getQLId() != -1) {
        Intent data = new Intent();
        data.putExtra("ql_id", Integer.toString(qlItem.getQLId()));
        if (getParent() == null) {
            setResult(Activity.RESULT_OK, data);
        }
        else {
            getParent().setResult(Activity.RESULT_OK, data);
        }
        finish();
    }
    finish();
}

Integer.toString (qlItem.getQLId ()) takes the value "1". This is important because I do not get "1" ...

I redefined the onActivityResult handler in Activity A with this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_QUICKLIST) {
        if (resultCode == Activity.RESULT_OK) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                int id = extras.getInt("ql_id");
            }
        }
    }
}

Unfortunately, extras.getInt ("ql_id") evaluates to "0". Why is this? It should be "1". I am clearly doing something wrong.

thanks for the help

+3
1

, . String int.

+2

Source: https://habr.com/ru/post/1757163/


All Articles