1

I have a ViewPager with 9 Fragments in it. Each Fragment got some amount of data loaded from Web. When I rotate screen or open another activity via Intent with all 9 fragments loaded they all call OnSaveInstance method, where each fragment data is serialized for future fragment recreation. It cause a little lags and message

Skipped 41 frames!  The application may be doing too much work on its main thread.

So OnSaveInstance in running on UI thread right? And serialization of 9 Fragments makes it skipping frames.

How can I avoid this laggs? Is there a way to do OnSaveInstance code in background?

Faceles
  • 453
  • 1
  • 5
  • 16

1 Answers1

2

You can get that message for many reasons and it does not automatically mean that your app is not performing well. Even if it's more than 41 frames in this case. For more information see this post:

Meaning of Choreographer messages in Logcat

update:

This post might help you in this case:

Why use Fragment#setRetainInstance(boolean)?

Keep in mind the Fragment life cycle when using setRetainInstance. See:

http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

For nice tips about memory management in Android see:

https://developer.android.com/training/articles/memory.html

For a very nice explanation of this method in detail see:

Understanding Fragment's setRetainInstance(boolean)

Community
  • 1
  • 1
Loolooii
  • 8,588
  • 14
  • 66
  • 90
  • For rotation its not critical lags. But for starting new Activity with shared elements transitions it kills all the "transitions" – Faceles Sep 29 '15 at 15:22
  • 1
    @Faceles I just added a link that might help you with this. – Loolooii Sep 29 '15 at 15:30
  • seems like this really helps me to avoid serialization. So I have a new question, what are consequences of using setRetainInstance? Fragments will be destroyed only by Android system, when leaking memory, right? And it's kinda rare case, so I can ignore it and replace OnSaveInstance logic with setRetainInstance logic? – Faceles Sep 29 '15 at 15:58
  • @Faceles Please see the links that I have added to the answer. Those should answer all of your questions (in particular the last one). The first two you can use as a reference for now or in the future. – Loolooii Sep 30 '15 at 12:22