The introduction

So why are there fragments? Android runs on a variety of devices, including phones with small screens, ultra-large tablets and even televisions, according to Hon Yang. In view of the gap in screen size, in many cases, they first develop a set of App for mobile phones, and then copy a copy, modify the layout to adapt to what super large screen tablet. You can use fragments in your activities whenever you want. When you have a complex interface business logic, you can write the logic into a Fragment. This way, you don’t have to write a bunch of control event handling code in your Activity. Accept the callback to handle the event.

Fragment life cycle

The Fragment life cycle is similar to the Activity life cycle, but with a few more callback methods.

OnAttach () : When an Activity interacts with a Fragment, we can provide data to the Fragment in the Activity using the fragment.setarguments () method, Then getArguments() in the Fragment onAttach () method gets a Bundle object. OnCreateView (): Creates a view corresponding to the Fragment, where you must return the view to the caller. How it differs from onCreate() : onCreate() creates the Fragment in which you can initialize anything other than the View. OnActivityCreated () Is called when the Activity’s onCreate() method is called. OnDetach () is called when the Fragment is unassociated with the Activity.

Fragments using

Fragment is used in two ways: static and dynamic

  • Static use of fragments refers to writing a custom Fragment to an XML layout file. There are three ways to provide ids for fragments using tags:

    Android: ID Attribute: Unique ID

    Android: Tag attribute: unique string

    If neither is provided, the system uses the container view ID.

  • Using fragments dynamically means manually adding, updating, and removing fragments from your code. Get FragmentManager, addFragment, set default display Fragment, start transaction, commit transaction.

The FragmentManager is used to maintain the Fragment queue and the rollback of Fragment transactions.

  • Add (): add a Fragment to an Activity remove(): remove a Fragment replace(), hide(), show() Fragment display has two methods: replace and hide and show. What is the difference between the two methods? Replace calls the Fragment’s life cycle, which means it will destroy the view and reload it. This is not recommended if your Fragment has a lot of data or view structures, which will increase your memory consumption. Hide and show show hidden fragments, resulting in V ‘isibliity VISIBLE and GONE.

  • The onHiddenChanged () method is not recommended for heavy business logic, but it is possible to refresh data during Fragment switching. If you want to hide or show the changed () method, you will need to use it.

  • Reason: Why does interface overlap exist? Because whenever we leave the Activity and switch to another APP, when memory is insufficient and the Fragment Activity is destroyed, the onSaveInstanceState() method will be called and the Fragment will be saved. When I return to the APP again, The fragment is restored with the savedInstanceState parameter in onCreate, resulting in interface overlap. SavedInstanceState = null; savedInstanceState = null; savedInstanceState = null; savedInstanceState = null Instead of re-adding the fragment, read the fragment directly from the previously saved data Tag.

public void onCreate(Bundle savedInstanceState) { fManager = getFragmentManager(); if (savedInstanceState ! = null) { fg1 = (AllOfficialAccountFragment) fManager.findFragmentByTag("fg1"); fg2 = (MovieOfficialAccountFragment) fManager.findFragmentByTag("fg2"); fg3 = (NewsOfficialAccountFragment) fManager.findFragmentByTag("fg3"); fg4 = (OtherOfficialAccountFragment) fManager.findFragmentByTag("fg4"); } super.onCreate(savedInstanceState); }Copy the code

2. Use getFragments() to obtain all fragments on the stack currently managed by the FragmentManager.

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) setContentView(R.layout.activity) TargetFragment targetFragment HideFragment hideFragment if (savedInstanceState ! = null) {/ / "memory reset" invoked when the List fragmentList = getSupportFragmentManager () getFragments () for (fragments fragments: fragmentList) { if(fragment instanceof TartgetFragment){ targetFragment = (TargetFragment)fragment }else if(fragment Instanceof HideFragment){HideFragment = (HideFragment) fragmentManager ().beginTransaction() Show (targetFragment). Hide (hideFragment). The commit ()} else {/ / normal targetFragment = targetFragment newInstance () HideFragment = hidefragment.newinstance () The tag can preach not getFragmentManager (). The beginTransaction (). The add (R.i, dc ontainer). The add (R.i d, container, hideFragment) .hide(hideFragment) .commit() } }Copy the code

This solves the interface overlap problem.

  • The next point is that we have a null pointer to getActivity() in the Fragment. Most of the reason for this problem is that the Fragment has been unassociated with the activity it is running on, calling the onDetach() method. We can define the Activity global variable in the Fragment and assign a value to the Activity when it is associated with the Fragment.
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.mActivity = (Activity)context;
}Copy the code

There are still many problems, such as Fragment nesting and transition animation, which have not been studied yet and will be gradually accumulated in the future. Should be recommended two articles of fragments, I now see the most complete: www.jianshu.com/p/d9143a92a… If there is something wrong in the article, I hope you can correct it. Thank you.