onActivityResult() Intent data is always null

前端 未结 3 513
耶瑟儿~
耶瑟儿~ 2021-02-12 03:16

Can somebody tell my why the Intent data is always null?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          


        
相关标签:
3条回答
  • 2021-02-12 03:40

    Make sure that your second activity is not finished before calling

    setResult(RESULT_OK, getIntent().putExtra(ProfileActivity.USER_DATA_EXTRA, constructUser()));
    

    i.e. you should call setResult before onPause, onStop, onDestroy, finish ... etc

    0 讨论(0)
  • 2021-02-12 04:01

    Posting here as a possible answer though may not be your issue exactly

    Ensure your activity returning passes back something like this:

    Intent returnIntent = new 
    returnIntent.putExtra("result", app);
    returnIntent.putExtra("element", element);
    if (app.getStatus() == 2){
        returnIntent.putExtra("update", true);
        // Tell the parent that everything went okay
        setResult(Activity.RESULT_OK, returnIntent);
        Log.i(TAG, "Returning, Result Success");
    } else {
        // Tell parent that nothing changed
        setResult(RESULT_CANCELED, returnIntent);
        Log.i(TAG, "Returning, Nothing changed");
    }
    finish();
    

    I spent a long time suffering from null intents being returned. For me it was because in onBackPressed I was calling super.onBackPressed() before the above code. When I put it after everything worked great. If onStop/onDestroy is called too early the opportunity to pass an intent back is blocked. This may be your issue

    0 讨论(0)
  • 2021-02-12 04:03

    You must return some data from the called Activity to calling Activity while finishing it.

    0 讨论(0)
提交回复
热议问题