4

I am facing trouble with android Oreo. My MainActivity has 4 fragments, which replace each other whenever the user presses tabs. Now the problem is, I am saving a value in a singleton instance in onPause. Whenever the user presses the next tab, onResume of that fragment is called before onPause, so I am not able to retrieve the value from the singleton correctly.

sorak
  • 2,607
  • 2
  • 16
  • 24
user3792429
  • 203
  • 1
  • 16

3 Answers3

6

Please use setReorderingAllowed as false to get the normal fragment lifecycle.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
fragmentTransaction.setReorderingAllowed(false);
} 

https://developer.android.com/reference/android/app/FragmentTransaction.html#setReorderingAllowed(boolean)

1

There is some problem with Fragment in oreo version.I created a separate app to check what lifecycle functions are called when we replace one fragment with other..Here is the log:

MainActivity onCreate ->
FragmentA oncreate ->
FragmentA oncreateView ->
MainActivity onStart ->
FragmentA onStart ->
MainActivity onResume ->
FragmentA onResume ->
Button Pressed;that replace fragment A with Fragment B
FragmentB onCreate ->
FragmentB onCreateView ->
FragmentB onStart ->
FragmentB onResume ->
FragmentA onPause ->
FragmentA onStop ->
FragmentA onDestroy ->

I was importing import android.app.Fragment.It worked when I replaced it with android.support.v4.app.Fragment .

kuzdu
  • 7,124
  • 1
  • 51
  • 69
user3792429
  • 203
  • 1
  • 16
0

onResume should called before onPause. If you want to save something before starting fragment do it in onStart. If you want to save something before leaving fragment do it in onStop. More info on Fragment lifecycle

Rafiqul Hasan
  • 3,324
  • 4
  • 20
  • 28