Android Jetpack
UI Components
Palette
Layout
Fragment
Emoji
Animation & transitions
Jetpack Components
introduction
Foundation
Architecture
Behavior
UI
Foundation Components
Android KTX
AppCompat
Auto
Benchmark
Multidex
Security
Test
TV
Wear OS by Google
Architecture Components
Data Binding
Lifecycles
LiveData
Navigation
Paging
Room
ViewModel
WorkManager
Behavior Components
CameraX
Download manager
Media & playback
Notifications
Permissions
Preferences
Sharing
Slices

Fragment



Fragment, Android Jetpack UI Components, A basic unit of composable UI


Android introduced fragments in Android 3.0 (API level 11), A Fragment is a behavior or a portion of user interface in a FragmentActivity. A FragmentActivity shall contain one or more reusable fragments.


Fragment will have its own life cycle, and directly affected by the host activity's lifecycle.


Fragment Life Cycle

  • onAttach()
  • onCreate()
  • onCreateView()
  • onActivityCreated()
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onDestroyView()
  • onDestroy()
  • onDetach()

There are two ways you can add a fragment to the activity layout

  • Declare the fragment inside the activity's layout file.
  • Programmatically add the fragment to an existing ViewGroup.

Declare the fragment inside the activity's layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.freedomtutorials.MyFragment"
            android:id="@+id/list"
            android:layout_weight="2"
            android:layout_width="2dp"
            android:layout_height="match_parent" />
</LinearLayout>

Fragments will has its own class to implement, and in the <fragment> definition of parent activity layout xml file, the android:name attribute specifies the Fragment class to instantiate.


Programmatically add the fragment to an existing ViewGroup.

 //get the instance of FragmentManager, Method of FragmentActivity subclass
 FragmentManager objFragmentManager = getSupportFragmentManager();

 //begin fragment transaction 
 FragmentTransaction objFragmentTransaction = objFragmentManager.beginTransaction();

 //create instance of your Fragment implementation
 MyFragment objMyfragment = new MyFragment();

 //add it to fragment transaction
 objFragmentTransaction.add(R.id.my_fragment_layout_container, objMyfragment);

 //commit 
 objFragmentTransaction.commit(); 

Finding and Access Fragment instance in Activity


MyFragment objMyfragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.my_fragment_layout_container);



Animation & transitions

Animation & transitions, Android Jetpack UI Components, Move widgets and transition between screens

posted on 2019-10-25 08:04:17 - Android Jetpack Tutorials


Emoji

Emoji, Android Jetpack UI Components, Enable an up-to-date emoji font on older platforms

posted on 2019-10-25 08:03:43 - Android Jetpack Tutorials


Fragment

Fragment, Android Jetpack UI Components, A basic unit of composable UI

posted on 2019-10-25 08:03:24 - Android Jetpack Tutorials


Prompt Examples

ChatGPT Prompt Examples

posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


Use Cases

Chat GPT Key Use Cases

posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


Prompt Frameworks

Prompt Frameworks

posted on 2023-06-21 19:33:06 - ChatGPT Tutorials