Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult Intent is null when passing Intent from Adapter

I am facing a strange issue while returning to an Activity with a Result, I am passing an Intent for startActivityForResult from an Adapter like this :

Intent i = new Intent(activity, EditInfoActivity.class);
i.putExtra("id", list.get(position).getID());
activity.startActivityForResult(i, 100);

and in second Activity i.e. in EditInfoActivity in my case on a Button click I am setting Result for first activity like this:

Intent i = getIntent();
i.putExtra("isDataChange", isDataChange);
setResult(100, i);
finish();

In Activity's onActivityResult method I am able to get result code but getting Intent null.

Why? anybody have any idea on this please share.

in Activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 100) {
    //Here data is null and app crash
    if (data.getExtras() != null && data.getBooleanExtra("isDataChange", false)) {
       recreate();
    }
  }
}
like image 974
Kapil Rajput Avatar asked Jan 25 '17 13:01

Kapil Rajput


People also ask

Can we call startActivityForResult from adapter?

Yes. You can call startactivityforresult() from adapter. There are two case- 1. Calling adapter from activity and need onActivityResult in activity.

What can I use instead of Startactivity for results?

We use startActivityForResult() to send and receive data between activities, in almost of our android projects. But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.

Can we use onActivityResult in fragment?

We can call startActivityForResult directly from Fragment but actually mechanic behind are all handled by Activity. Once you call startActivityForResult from a Fragment, requestCode will be changed to attach Fragment's identity to the code.

How does onActivityResult work on Android?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.


3 Answers

First, you need to start the Activity with a REQUEST_CODE:

// Here we set a constant for the code.
private final int REQUEST_CODE = 100;

Intent i = new Intent(activity, EditInfoActivity.class);
i.putExtra("id", list.get(position).getID());
activity.startActivityForResult(i, REQUEST_CODE);

Then you need to send RESULT_OK when finishing EditInfoActivity:

Intent i = getIntent();
i.putExtra("isDataChange", isDataChange);
setResult(RESULT_OK, i);
finish();

Then handle the result on your first activity with this:

Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // REQUEST_CODE is defined as 100
  if (resultCode == RESULT_OK && requestCode == 100) {
     // do process
  }
}
like image 94
ישו אוהב אותך Avatar answered Oct 05 '22 09:10

ישו אוהב אותך


setResult TAKES RESULT_CODE instead of REQUEST_CODE.

Replace your code with this, May be it will solve your problem.

setResult(RESULT_OK, i);

And in yout onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
   //Here data is null and app crash
        if (data != null && data.getBooleanExtra("isDataChange", false)) {
            recreate();
        }
    }
}
like image 21
Vishal Chhodwani Avatar answered Oct 05 '22 09:10

Vishal Chhodwani


Two mistakes. You are passing the intent that was used to launch the activity you are finishing. Use new Intent() instead.

When setting activity result you should use result codes, not a request code setResult(RESULT_OK) alternatively RESULT_CANCELED and handle the response accordingly.

like image 29
Andrej Jurkin Avatar answered Oct 05 '22 07:10

Andrej Jurkin