2

i have two fragments A and B.here I am Replacing fragment B from A And then fragment A from B in between i am showing toast in onPause method and onResume method but some how its not working could any one explain me why with code ?

umesh vashisth
  • 339
  • 3
  • 16

1 Answers1

1

The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.

Managing the lifecycle of a fragment is a lot like managing the lifecycle of an activity.

  • Resumed :-> The fragment is visible in the running activity.
  • Paused :-> Another activity is in the foreground and has focus, but the activity in which this fragment lives is still visible (the foreground activity is partially transparent or doesn't cover the entire screen).
  • Stopped :-> The fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed.

for more information read from android docs

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • so how am i suppose to call them can please give me some example? – umesh vashisth Sep 02 '17 at 11:04
  • @umeshvashisth fragments onResume() or onPause() will be called only when the Activities onResume() – AskNilesh Sep 02 '17 at 11:07
  • @umeshvashisth you can retain the state of a fragment using a Bundle, in case the activity's process is killed and you need to restore the fragment state when the activity is recreated. You can save the state during the fragment's onSaveInstanceState() callback and restore it during either onCreate(), onCreateView(), or onActivityCreated(). For more information about saving state, see the Activities document. – AskNilesh Sep 02 '17 at 11:07
  • sir, i read that doc ...but i dont have idea how to call them thats why i am asking for some code example so i can understand it better.thanks for reply. – umesh vashisth Sep 02 '17 at 11:09
  • check this ans @umeshvashisth https://stackoverflow.com/a/44078724/7666442 – AskNilesh Sep 02 '17 at 11:10