Fragment of Android interview questions

Preface

Autumn recruits are coming soon, Ji Meng will prepare a set of Android primary interview questions before the end of the National Day holiday. I hope it will be helpful to everyone.


Tip: The following is the content of this article

Fragment

1. How to switch fragement (not re-instantiate)

Looking at the official Android Doc and the source code of some components, I found that the replace() method is just an easy way to use when the previous Fragment is no longer needed.

The correct switching method is add(), when switching, hide(), add() another Fragment; when switching again, only need to hide() the current and show() the other.

In this way, multiple Fragments can be switched without reinstancing:


2. Advantages of Fragment

(1) Fragment can enable you to separate activity into multiple reusable components, each with its own life cycle and UI.

(2) Fragment can easily create dynamic and flexible UI design, which can be adapted to different screen sizes. From mobile phones to tablets.

(3) Fragment is an independent module, tightly bound with activity. Can dynamically remove, join, exchange, etc. during operation.

(4) Fragment provides a new way for you to unify your UI on different Android devices.

(5) Fragment solves the problem of unsmooth switching between activities and light switching. (6) Fragment replaces TabActivity for navigation, with better performance.

(7) Fragment A new method of using nested fragments is added in version 4.2, which can generate better interface effects.


3. How does Fragment achieve the push and pop effect similar to the Activity stack?

Fragment's thing manager maintains a doubly linked list structure, which can record the fragments we add and replace each time, and then when we click the back button, it will automatically help us realize the stack operation.


4. The difference between the replace and add methods of Fragment

Fragment itself does not have replace and add methods. The understanding here should be what is the difference when using FragmentManager's replace and add methods to switch Fragment.

An architecture we often use is to switch Fragments through RadioGroup, and each Fragment is a functional module.

The Fragment container is a FrameLayout. When adding, all the Fragments are superimposed on the FrameLayout layer by layer, and when replacing, first remove the other Fragments in the container and then add the current Fragment to the container.

Only one Fragment type can be added to a Fragment container. If it is added multiple times, an exception will be reported and the program will be terminated. Replacement does not matter, just switch at will.

Because the Fragment added by the add method, each Fragment can only be added once, so if you want to achieve the switching effect, you need to use the combination of the hide and show methods of the Fragment. Show the show to be displayed and hide the others. The life cycle of fragments in this process has not changed. By switching Fragment by replace, the onDestroyView of the previous Fragment and the onCreateView, onStart, and onResume methods of the new Fragment will be executed each time.

Based on the above different characteristics, we must use our view and data in conjunction with the life cycle operation.


5. How to transfer values ​​between Fragment and Activity

  1. Activity passes value to Fragment:

Put the value to be passed into the bundle object; create the fragment object fragment in the Activity, and
pass it to the fragment by calling fragment.setArguments();
in the fragment , you can get the bundle object by calling getArguments() Value.

  1. Fragment passes values ​​to Activity:

Call getFragmentManager() in Activity to get fragmentManager, call findFragmentByTag(tag) or through findFragmentById(id)
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);

By means of callbacks, define an interface (which can be defined in the Fragment class). There is an empty method in the interface. The method of the interface is called when needed in the fragment. The value can be placed in this method as a parameter, and then the Activity can be implemented This interface will inevitably rewrite this method, so that the value is passed to the Activity


6.Fragment life cycle

  1. onAttach(Contextcontext): It is called when the Fragment is associated with the Activity, and it is called only once. In this callback, we can convert the context into Activity and save it, so as to avoid the situation of frequently calling getAtivity() to obtain the Activity later, and avoid the exception of getAtivity() empty in some cases (in the case of separation of Activity and Fragment) . At the same time, the incoming Arguments can also be extracted and parsed in this callback. It is strongly recommended to pass parameters to Fragment through setArguments, because Fragment will not save relevant attributes when the application is recycled by the system.
  2. onCreate: Called when the Fragment is initially created, similar to the onCreate of Activity.
  3. View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState): Called when preparing to draw the Fragment interface, the return value is the root view of the Fragment to draw the layout, of course, it can also return null. Note that attachToRoot must be specified as false when using the inflater to build the View, because Fragment will automatically add the view to the container, and attachToRoot to true will repeatedly add an error. onCreateView is not necessarily called. When adding a Fragment without an interface, it will not be called, such as calling the add(Fragment fragment, String tag) method of FragmentTransaction.
  4. onActivityCreated: Called when the onCreated of the Activity is executed.
  5. onStart(): Called when the Fragment is visible to the user, provided that the Activity has been started.
  6. onResume(): Called when the Fragment and the user can interact before, provided that the Activity has been resumed.
  7. onPause(): Called when the Fragment and the user cannot interact before.
  8. onStop(): Called when the Fragment is not visible.
  9. onDestroyView(): Called when the Fragment related view hierarchy is removed.
  10. onDestroy(): Called when the Fragment status is finally clear.
  11. onDetach(): Called when Fragment and Activity are disassociated.

7. ViewPager's impact on Fragment life cycle

ViewPager+Fragment is a relatively common combination, generally used with FragmentPagerAdapter or FragmentStatePagerAdapter of ViewPager. However, ViewPager has a caching mechanism in order to prevent the sliding from being stuck. By default, ViewPager will create and cache the pages (such as Fragment) on the left and right sides of the current page. At this time, both the left and right Fragments will execute the life cycle from onAttach->...->onResume. It is clear that the Fragment is not displayed but it has reached onResume. In some cases, problems will occur. For example, when the data is loaded, whether the Fragment is visible, etc.


About sorting out

When everything is sorted, it will be sorted into pdf format for easy reading. The file is obtained as shown below (after October 8)!


Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42761395/article/details/108897377