-->

Crashes after startActivityForResult in API 27

2019-07-29 05:17发布

问题:

After updating to API 27 and Support library 27.0.2 suddenly I get a lot of these stack traces in Crashlytics:

Fatal Exception: java.lang.IllegalArgumentException
    at android.os.Parcel.readException(Parcel.java:1544)
    at android.os.Parcel.readException(Parcel.java:1493)
    at android.app.ActivityManagerProxy.isTopOfTask(ActivityManagerNative.java:5108)
    at android.app.Activity.isTopOfTask(Activity.java:5688)
    at android.app.Activity.startActivityForResult(Activity.java:3973)
    at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(Source:54)
    at android.support.v4.app.FragmentActivity.startActivityForResult(Source:67)

I call this like:

ActivityOptions options = ActivityOptions.makeCustomAnimation(activity, R.anim.slide_in_from_right, R.anim.fade_out);
startActivityForResult( intent, REQ_ACTION, options.toBundle());

I cannot read the source code as it is not released yet. I even tried to replace and use android-26 code, but it's different.

There is a warning for the above call saying that BaseFragmentActivityApi16.startActivityForResult can only called from the same library group, so I fixed it by using ActivityCompat, but I don't think it will solve the crash problem.

Is this a platform issue or can I fix this?

Edit

if (Build.VERSION.SDK_INT >= 21) {
    ActivityOptions options = ActivityOptions.makeCustomAnimation(activity, R.anim.slide_in_from_right, R.anim.fade_out);
    startActivityForResult(intent, REQ_ACTION, options.toBundle());
} else {
    ActivityOptions options = ActivityOptions.makeCustomAnimation(activity, R.anim.slide_in_from_right, R.anim.fade_out);
    ActivityCompat.startActivityForResult(this, intent, REQ_ACTION, options.toBundle());
}

If I change it to the above according to the link in my comment, Android Studio complaining like above. This might be related to the problem.

回答1:

You can try this code.

startActivityForResult( intent, REQ_ACTION)
overridePendingTransition(R.anim.slide_in_from_right,  R.anim.fade_out);


回答2:

Old post but unanswered, so here what I found in 21+

Make sure you are looking for startActivityForResult under activity object. Under Context object you can find startActvity but you will not see startActivityForResult method.

If your context is Context class but is an activity then make sure you cast it to Activity.

Context context = ...;
context.startActivityForResult(...); // this method will not exist
((Activity)context).startActivityForResult(...); // this method should be ok


回答3:

Use ActivityOptionsCompat instead of ActivityOptions for below api 21.

ActivityOptionsCompat is a helper class for accessing features in ActivityOptions in a backwards compatible fashion.

if (Build.VERSION.SDK_INT >= 21) {
  ActivityOptions options = ActivityOptions.makeCustomAnimation(activity, 
  R.anim.slide_in_from_right, R.anim.fade_out);
  startActivityForResult(intent, REQ_ACTION, options.toBundle()); 
  } else {
   ActivityOptionsCompat options = ActivityOptionsCompat
  .makeCustomAnimation(activity,R.anim.slide_in_from_right,R.anim.fade_out);
   ActivityCompat.startActivity(this, intent, options.toBundle());
}

Hope this will help you.