5

Implemented activity to activity shared element transition. It works fine but receiving crashes on very few devices that are running >=LOLLIPOP.

Report:

Fatal Exception: java.lang.IllegalArgumentException
       at android.os.Parcel.readException(Parcel.java:1550)
       at android.os.Parcel.readException(Parcel.java:1499)
       at android.app.ActivityManagerProxy.isTopOfTask(ActivityManagerNative.java:4654)
       at android.app.Activity.isTopOfTask(Activity.java:5557)
       at android.app.Activity.startActivityForResult(Activity.java:3903)
       at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
       at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:65)
       at android.app.Activity.startActivity(Activity.java:4146)
       at com.mypackage.Activity1.method1(Activity1.java:414). 

tried this:

Intent intent = new Intent(Activity1.this, Activity2.class);
     ActivityOptionsCompat options = ActivityOptionsCompat.
                    makeSceneTransitionAnimation(Activity1.this,
                            logoImageView,
                            ViewCompat.getTransitionName(logoImageView));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                startActivity(intent, options.toBundle());
            } else {
                startActivity(intent);
            }
            overridePendingTransition(R.anim.stay, R.anim.stay); 

then this from this sof IllegalArgumentException in ActivityManagerProxy:

Intent intent = new Intent(Activity1.this, Activity2.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    ActivityOptions options = ActivityOptions
            .makeSceneTransitionAnimation(Activity1.this,
                    logoImageView,
                    getString(R.string.splashLogoSharedTransition));
    startActivity(intent, options.toBundle());
} else {
    ActivityOptionsCompat options = ActivityOptionsCompat.
            makeSceneTransitionAnimation(SplashActivity.this,
                    logoImageView,
                    getString(R.string.splashLogoSharedTransition));
    ActivityCompat.startActivity(SplashActivity.this, intent, options.toBundle());
}
overridePendingTransition(R.anim.stay, R.anim.stay);  

Crash happens with the both the codes at:

startActivity(intent, options.toBundle());  

Ever faced ? Any hints ?

cgr
  • 4,578
  • 2
  • 28
  • 52
  • shouldn't it be `new Intent(Activity1.this, Activity2.class)`? – ShahiM Nov 02 '17 at 11:24
  • @ShahiM yes, copy mistake. Corrected it. – cgr Nov 02 '17 at 11:34
  • and does `logoImageView` have the attribute : `android:transitionName="myTransition"` – ShahiM Nov 02 '17 at 11:38
  • Yes, it has. And even the image view in the Activity2 has the same transition. Also v21/styles.xml has true – cgr Nov 02 '17 at 11:39
  • I don't see any more possible issues with the code. I have the exact same code working without issues in one of my projects. – ShahiM Nov 02 '17 at 12:23
  • Just a guess here: `android.os.Parcel.readException` may indicate that the issue is in Activity2, i.e when the bundle is read. Try probing in that direction and hopefully you can figure out the issue. – ShahiM Nov 02 '17 at 12:26
  • I see the bundle is null here: protected void onCreate(Bundle savedInstanceState) in the Activity2 – cgr Nov 02 '17 at 17:36
  • should we mention true even in res/values/styles.xml ? Does this affect anyway to lollipop and above. It should not technically but that's something missing in my code. – cgr Nov 02 '17 at 17:47
  • I am using `true` in my project in `styles-v21`. please try that instead. – ShahiM Nov 03 '17 at 07:55

2 Answers2

3

It seems like you are using Window.FEATURE_CONTENT_TRANSITIONS. But Instead, you should be using Window.FEATURE_ACTIVITY_TRANSITIONS.

In your styles-v21.xml, add:

<item name="android:windowActivityTransitions">true</item>
<!-- optional -->
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item> 

From the Docs :

Window.FEATURE_CONTENT_TRANSITIONS:

Enables Activities to run Activity Transitions either through sending or receiving ActivityOptions bundle created with makeSceneTransitionAnimation(Activity, Pair[]) or makeSceneTransitionAnimation(Activity, View, String).

Window.FEATURE_ACTIVITY_TRANSITIONS:

Flag for requesting that window content changes should be animated using a TransitionManager.

The TransitionManager is set using setTransitionManager(TransitionManager). If none is set, a default TransitionManager will be used.

See this post for more info.

Community
  • 1
  • 1
ShahiM
  • 3,179
  • 1
  • 33
  • 58
  • my v21/styles.xml had true. It's a legacy code and I am not sure for what it has been added. Is it okay if I add true also along with content transitions flag in styles? – cgr Nov 03 '17 at 08:46
  • Yes. Absolutely. – ShahiM Nov 03 '17 at 10:19
  • cool. Thanks for info. I will add it and see if I will get any more crashes. I think better I should have followed Google doc instead of a blog post which was kinda outdated and was mentioning contentTransition. – cgr Nov 03 '17 at 10:21
  • I'll post this fix to play store and see. Crash didn't occur in my phone but from few user's phones. It will work I believe, I'll accept. – cgr Nov 04 '17 at 07:43
  • Okay. Cool. . – ShahiM Nov 04 '17 at 07:44
  • Thanks for the help and answer. Tested in production and so far no crashes. – cgr Nov 07 '17 at 07:23
  • I still get crashes in production. Not sure having both contentTransition and activityTransition enabled in v21/styles.xml is causing the issue. Happens only on very few devices. – cgr Nov 08 '17 at 20:05
  • 1
    I have added try for startActivity(intent, options.toBundle()); and in catch, I started activity without activityOptions so just called startActivity(intent);. Added an event in catch() block to know if crash still happens with this answer. I received 8 of those event so far which means this solution did not help fixing crash. But my app does not crash due to this as I caught Exception. :) – cgr Nov 09 '17 at 07:46
0

According to this post you shouldn't use ActivityOptionsCompat above API 21: https://stackoverflow.com/a/42455484/1067763

I don't use it, but I still have this crash:

Crashes after startActivityForResult in API 27

I think it's still using the wrong version somehow.

Still, knowing this you might be able to solve your issue.

Herrbert74
  • 2,578
  • 31
  • 51
  • I have not used as per my question. Anyway, the crash I am getting is for LOLLIPOP and above when using ActivityOptions as I explained in my question. – cgr Dec 07 '17 at 12:46
  • Sorry, I didn't notice that the second part is about the very same post. But now I noticed that you are using both ActivityOptions and overridePendingTransitions. I wonder if that could be the source of the problem? – Herrbert74 Dec 07 '17 at 14:15
  • But I need overridePendingTransitions for my case. – cgr Dec 07 '17 at 15:57