3

I do understand bit about fragment lifecycle, but I am finding me unable to visualize well the flow chart(or control diagram). When we move from one fragment to other. eg

            TheFragment tf6 = new TheFragment(); // TheFragment class extends Fragmet 
            adapter.addFragment(tf6 , naam[q--]);
            tf6.setArguments(args);

            TheFragment tf5 = new TheFragment(); another fragment
            adapter.addFragment(tf5 , naam[q--]);
            tf5.setArguments(args);

           `viewPager.setAdapter(adapter);`

Can anyone explain me just a flow of control or diagram in which we move from one fragment to other, so which method of which fragment called in a sequence.(Like OnPouse() of frag1 to onResume() of frag2 (for example)) Thanks in advance for your explanation.

Saket
  • 79
  • 1
  • 5

2 Answers2

3

You can do it by creating a fragment class and overriding all its methods with a log. Just navigate and see which log appears when.

sunil
  • 6,444
  • 1
  • 32
  • 44
swati kapoor
  • 131
  • 13
-1

Android fragments have their own life cycle very similar to an android activity. See the image

onAttach() The fragment instance is associated with an activity instance.In this method you get the reference of the activity which called the fragment.

onCreate() The system calls this method when creating the fragment.

onCreateView() The system calls this callback when it's time for the fragment to draw its user interface for the first time.

onStart() This method is called once the fragment gets visible.

onDestroyView() Fragment view will destroy after call this method

See this tutorial for better understanding of life cycle.

Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26
  • Some parts of this answer are misleading. onStart() doesn't necessarily mean "the fragment is visible". To test; add a fragment to stack with `FragmenManager.add()` then pop back with back button `onStart()` will not be called. – Farid Mar 08 '19 at 12:46