android - onActivityResult为什么不执行?
天蓬老师
天蓬老师 2017-04-17 17:29:24
0
4
511

Activity A:

int tag  = 0;
startActivityForResult(new Intent(getApplication(), LoreAddActivity.class).putExtra("tag1", tag), 100);

@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
    super.onActivityResult(arg0, arg1, arg2);
    if(arg1==RESULT_OK){
        switch (arg0) {
        case 100:
            tag2 = arg2.getIntExtra("tag2", 0);
            break;
        default:
            break;
        }
    }
}

Activity B:

int tag = getIntent().getIntExtra("tag1",0);

Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putInt("tag2", tag);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(4)
Peter_Zhu

There may be something wrong in the intent. Try new Intent(XXX.this,LoreAddActivity.class);

getApplication() though its referring to Application object but the
Application class extends Context class, so it can be used to offer
application context.

The application object returned by getApplication also inherits context, so you can use it like this. But if the source is not explicitly specified, then when receiving the callback, that is, setResult, B does not know where the intent comes from. The above is my understanding and is for reference only. If there are any errors, please point them out

巴扎黑

As long as Activity A is returned, onActivityResult will be executed. You can use Log or debug to confirm where onActivityResult is not executed

PHPzhong

What are the startup modes of the two activities?

黄舟
startActivityForResult(new Intent(getApplication(), LoreAddActivity.class).putExtra("tag1", tag), 100);

This sentence uses getApplication. Activity starts in standard mode by default. An Activity started in this mode will enter the task stack of the Activity that started it by default. The Context obtained by the getApplication method does not contain task stack information. Therefore, it is best not to use getApplication when starting an Activity, and replace it with the Context of the Activity.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template