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.
Asked
Active
Viewed 1,889 times
4
-
I recommend you to go through fragment & activity lifecycle refer this https://developer.android.com/guide/components/fragments.html – Akshay Katariya Mar 21 '18 at 03:54
-
I read,Still dont know why onPause is called after onStop. – user3792429 Mar 21 '18 at 04:01
3 Answers
6
Please use setReorderingAllowed as false to get the normal fragment lifecycle.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
fragmentTransaction.setReorderingAllowed(false);
}

Sathish Kumar
- 71
- 4
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
-
I am talking about the case when one fragment is replaced by another..after 1st fragment onResume,when I press the other tab for replacing it with other fragment..1st fragment pause should have been called first and than 2nd fragment onResume – user3792429 Mar 21 '18 at 04:10
-
-
I have tried saving in onStop as well,It works perfectly fine in all android version except oreo – user3792429 Mar 21 '18 at 04:33
-
can you give me fragment implementation code and lifecycle logcat? – Rafiqul Hasan Mar 21 '18 at 05:18