The first line of code reading notes

debris

  • A fragment is a test UI fragment embedded in an activity
  • It’s widely used on tablets

Based on using

  • Create two new shards

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Button"
            />
    
    </LinearLayout>
    Copy the code
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:background="#00ff00"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"
            android:text="This is a right fragment"
            />
    </LinearLayout>
    Copy the code
  • Create a LeftFragmetn class that inherits from Fragment

    public class LeftFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fragment, container, false); return view; }}Copy the code
    • Support – v4 fragments support repository versions compatible with android. Support. The v4. App. The fragments
    • Overwrite the onCreateView() method to dynamically load the left_fragment layout in through the inflate() method of the LayoutInflater
  • Create the RightFragment class in the same way

public class RightFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.right_fragment, container, false); return view; }}Copy the code
  • Modify activity_main. XML
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <fragment
            android:id="@+id/lft_fragment"
            android:name="com.example.fragmenttest.LeftFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
        <fragment
            android:id="@+id/right_fragment"
            android:name="com.example.fragmenttest.RightFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
    </LinearLayout>
    Copy the code
    • use<fragment>The label
    • android:name
      • Explicitly specifies the name of the shard class to add
      • The package name of the class is also added

Dynamic Fragmentation

  • New another_right_fragment. XML
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:background="#ffff00"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"
            android:text="This is another right fragment"
            />
    
    </LinearLayout>
    Copy the code
  • New AnotherRightFragment class
    public class AnotherRightFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.another_right_fragment, container, false); return view; }}Copy the code
  • Modify activity_main.xml to replace right_fragment with
    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    Copy the code
  • Modify MainActivity to add content to FrameLayout
    public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(this); replaceFragment(new RightFragment()); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: replaceFragment(new AnotherRightFragment()); break; default: break; } } private void replaceFragment(Fragment fragment) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.right_layout, fragment); transaction.commit(); }}Copy the code
  • Dynamic fragmentation can be divided into five steps
    1. Create the shard instance to be added
    2. To obtainFragmentManagerIn the activity can be directly through the callgetSupportFragmentManager()In the form of
    3. Start a transactionFragmentTransaction, by callingbeginTransaction()Methods open
    4. Add or replace fragments to a container, generally usedreplace()Method implementation, which needs to be passed into the containeridAnd the shard instance to be added
    5. Commit transaction, callcommit()Methods to complete

Simulate the return stack in the fragment

  • FragmentTransactionProvided in theaddToBackStack()methods
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
    Copy the code
    • Use to add transactions to the return stack
    • Receiving a name that describes the state of the return stack is generally passed null

Communication between debris and activity

  • FragmentTransactionProvided in thefindFragmentById()methods
    RightFragment rightFragment = (RightFragment) getFragmentManager()
        .findFragmentById(R.id.right_fragment);
    Copy the code
    • An instance used to get fragments from the layout file
  • getActivity()
    MainActivity activity = (MainActivity) getActivity();
    Copy the code
    • Gets the active instance associated with the current fragment
    • Use when needed in fragmentsContextObject can also be used

      The retrieved activity itself is the Context object

The life cycle of fragments

Fragmentation states and callbacks

  1. Running state
    • Visible, and its associated activity is running
  2. hold
    • When an activity goes into a suspended state, the visible fragments associated with it go into a suspended state
  3. Stop state
    • When an activity goes into a stopped state, the fragments associated with it go into a stopped state
    • By calling FragmentTransactionremove(),replace()Method to remove the fragment from the activity,

      But called before the transaction commitsaddToBackStack()Method, the fragment enters the stopped state
    • Completely invisible and may be reclaimed by the system
  4. Destruction of state
    • When an activity is destroyed, the fragment associated with it goes into the destroyed state
    • By calling FragmentTransactionremove(),replace()Method to remove the fragment from the activity,

      But it is not called before the transaction commitsaddToBackStack()Method, the fragment enters the destruction state
  • onAttach(): called when a fragment is associated with an activity

    onCreateView(): called when creating a view for the shard (loading the layout)

    onActivityCreated(): called when ensuring that the activity associated with the shard must have been created

    onDestroyView(): called when the view associated with the shard is removed

    onDetacch(): called when fragmentation and activity are disassociated
  • Full life cycle of shard -> onAttach() -> onCreate() -> onCreateView() -> onActivityCreated() -> onStart() -> onResume() -> Shard Activation (Remove) -> onPause() -> onStop() -> onDestroyView() -> onDestroyView() -> onDestroy() -> onDetach()

Tips for dynamically loading layouts

qualifiers

  • Quilifier
  • Devices with a new layout-Large folder screen in the RES directory that is considered large will automatically load the layout under the Layout-Large folder
  • The size of the
    • small, normal, large, xlarge
  • The resolution of the
    • ldpi, mdpi, hdpi, xhdpi, xxhdpi
  • The direction of
    • Land (landscape), Port (portrait)

Minimum width qualifier

  • Smallest-width Qualifier
  • Dp for the unit
  • E.g. create a folder layout-sw600d