SlideShare a Scribd company logo
1 of 28
Easy to understand
Fragment
What is fragment?
Is it another type of Activity?
Does it replace Activity?
Or is it a new component ?
What is fragment?
Is it another type of Activity?
Does it replace Activity?
Or is it a new component ?
It is a modular section of an activity!!
Relationship with Activity
Embedded in an activity surely
Depend on activity lifecycle
Inside activity’s view hierarchy
Fragment advantage
Multi-pane UI support
Reuse component
Add or remove component dynamically
When to use fragment
Multi-pane UI support
How to use fragment
1 <fragment
2 android:id="@+id/list"
3 android:name=“com.new.ExampleFragment"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent" />
Inside the activity’s layout
Add fragment programmatically
1 FragmentManager fm = getFragmentManager();
2 FragmentTransaction ft = fm.beginTransaction();
3 ExampleFragment fragment = new ExampleFragment();
4 ft.add(R.id.fragment_container, fragment);
5 ft.commit();
1 <FrameLayout
2 android:id="@+id/fragment_container"
3 android:layout_width="wrap_content"
4 android:layout_height=“wrap_content"/>
Fragment Transactions
1 FragmentTransaction ft = fm.beginTransaction();
2 ft.add(R.id.fragment_container, fragment);
3 ft.replace(R.id.fragment_container, fragment);
Modify fragment
4 ft.show(fragment)
5 ft.hide(fragment)
6 ft.remove(fragment)
Modify fragment already in activity
Set transition style
7 ft.setTransition(TRANSIT_FRAGMENT_CLOSE)
8 ft.setCustomAnimations(entet,exit)
Fragment Communication
Just like activity communication directly?
Fragment Communication
Just like activity communication directly?
Communicate through activity !
Main Activity
fragment
Controller
Main Activity
fragment
Controller
1 public class ChannelDetailFragment extends Fragment {
2 private OnChannelClickListener mCallback;
3
4 public interface OnChannelClickListener {
5 public void onChannelClick(int channelNumber);
6 }
7 @Override
8 public void onAttach(Activity activity) {
9 super.onAttach(activity);
10 try {
11 mCallback = (OnChannelClickListener) activity;
12 } catch (ClassCastException e) {
13 throw new ClassCastException(activity.toString()
14 + " must implement OnChannelClickListener");
15 }
16 }
17 public boolean onClick(View v) {
18 mCallback.onChannelClick(channelNumber);
19 }
In fragment file
In activity file
1 public class Activity extends Activity
2 implements OnChannelClickListener {
3
4 @Override
5 public void onChannelClick(int channelNumber){
6 // do something or open another fragment.
7 }
8 }
Fragment view
Use setContentView() to set view?
Fragment view
Use setContentView() to set view?
1 @Override
2 public View onCreateView(LayoutInflater inflater, ViewGroup
3 container, Bundle savedInstanceState) {
4 View root = inflater.inflate(R.layout.fragment_layout,
5 container, false);
6 //init other view
7 return root;
8 }
Use onCreateView callback to set view !
Fragment tips
Toast.makeText(getActivity(), "Mission complete",
Toast.LENGTH_LONG);
Get context from activity
Add to BackStack
1 FragmentManager fm = getFragmentManager();
2 FragmentTransaction ft = fm.beginTransaction();
3 ft.replace(R.id.fragment_container, fragment);
4 ft.addToBackStack(null);
Import v4 or origin version
1 import android.support.v4.app.FragmentActivity;
2 import android.support.v4.app.FragmentManager;
3 import android.support.v4.app.FragmentTransaction;
4 import android.support.v4.app.Fragment;
1 import android.app.Activity;
2 import android.app.FragmentManager;
3 import android.app.FragmentTransaction;
4 import android.app.Fragment;
Use new Fragment() or
Fragment.newInstance()?
1 ChannelFragment fragment = new ChannelFragment();
2 Bundle args = new Bundle();
3 args.putInt(CHANNEL_PAGE_INDEX, index);
4 fragment.setArguments(args);
1 public static ChannelFragment newInstance(int index) {
2 ChannelFragment fragment = new ChannelFragment();
3 Bundle args = new Bundle();
4 args.putInt(CHANNEL_PAGE_INDEX, index);
5 fragment.setArguments(args);
6 return fragment;
7 }
ChannelFragment.newInstance(index);
Use static factory method for better!
Thank you!
Fantasy

More Related Content

Similar to 深入淺出談Fragment

Similar to 深入淺出談Fragment (20)

Tk2323 lecture 6 fragment (new)
Tk2323 lecture 6   fragment (new)Tk2323 lecture 6   fragment (new)
Tk2323 lecture 6 fragment (new)
 
Short Intro to Android Fragments
Short Intro to Android FragmentsShort Intro to Android Fragments
Short Intro to Android Fragments
 
Android App Development - 06 Fragments
Android App Development - 06 FragmentsAndroid App Development - 06 Fragments
Android App Development - 06 Fragments
 
Fragments anyone
Fragments anyone Fragments anyone
Fragments anyone
 
What the fragments
What the fragmentsWhat the fragments
What the fragments
 
Android development session 4 - Fragments
Android development   session 4 - FragmentsAndroid development   session 4 - Fragments
Android development session 4 - Fragments
 
Fragment
Fragment Fragment
Fragment
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
04 activities - Android
04   activities - Android04   activities - Android
04 activities - Android
 
Android
AndroidAndroid
Android
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)
 
STYLISH FLOOR
STYLISH FLOORSTYLISH FLOOR
STYLISH FLOOR
 
Lesson 4
Lesson 4Lesson 4
Lesson 4
 
Activity
ActivityActivity
Activity
 
Activity
ActivityActivity
Activity
 
Activity
ActivityActivity
Activity
 
Activity
ActivityActivity
Activity
 
Mobile application development: part 1: Andriod Vs IOS
Mobile application development: part 1: Andriod Vs IOS Mobile application development: part 1: Andriod Vs IOS
Mobile application development: part 1: Andriod Vs IOS
 
Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android Development
 
Android 3
Android 3Android 3
Android 3
 

Recently uploaded

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Recently uploaded (20)

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

深入淺出談Fragment

  • 2. What is fragment? Is it another type of Activity? Does it replace Activity? Or is it a new component ?
  • 3. What is fragment? Is it another type of Activity? Does it replace Activity? Or is it a new component ? It is a modular section of an activity!!
  • 4. Relationship with Activity Embedded in an activity surely Depend on activity lifecycle Inside activity’s view hierarchy
  • 5. Fragment advantage Multi-pane UI support Reuse component Add or remove component dynamically
  • 6. When to use fragment
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13. How to use fragment 1 <fragment 2 android:id="@+id/list" 3 android:name=“com.new.ExampleFragment" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" /> Inside the activity’s layout
  • 14. Add fragment programmatically 1 FragmentManager fm = getFragmentManager(); 2 FragmentTransaction ft = fm.beginTransaction(); 3 ExampleFragment fragment = new ExampleFragment(); 4 ft.add(R.id.fragment_container, fragment); 5 ft.commit(); 1 <FrameLayout 2 android:id="@+id/fragment_container" 3 android:layout_width="wrap_content" 4 android:layout_height=“wrap_content"/>
  • 15. Fragment Transactions 1 FragmentTransaction ft = fm.beginTransaction(); 2 ft.add(R.id.fragment_container, fragment); 3 ft.replace(R.id.fragment_container, fragment); Modify fragment 4 ft.show(fragment) 5 ft.hide(fragment) 6 ft.remove(fragment) Modify fragment already in activity Set transition style 7 ft.setTransition(TRANSIT_FRAGMENT_CLOSE) 8 ft.setCustomAnimations(entet,exit)
  • 16. Fragment Communication Just like activity communication directly?
  • 17. Fragment Communication Just like activity communication directly? Communicate through activity !
  • 20. 1 public class ChannelDetailFragment extends Fragment { 2 private OnChannelClickListener mCallback; 3 4 public interface OnChannelClickListener { 5 public void onChannelClick(int channelNumber); 6 } 7 @Override 8 public void onAttach(Activity activity) { 9 super.onAttach(activity); 10 try { 11 mCallback = (OnChannelClickListener) activity; 12 } catch (ClassCastException e) { 13 throw new ClassCastException(activity.toString() 14 + " must implement OnChannelClickListener"); 15 } 16 } 17 public boolean onClick(View v) { 18 mCallback.onChannelClick(channelNumber); 19 } In fragment file
  • 21. In activity file 1 public class Activity extends Activity 2 implements OnChannelClickListener { 3 4 @Override 5 public void onChannelClick(int channelNumber){ 6 // do something or open another fragment. 7 } 8 }
  • 23. Fragment view Use setContentView() to set view? 1 @Override 2 public View onCreateView(LayoutInflater inflater, ViewGroup 3 container, Bundle savedInstanceState) { 4 View root = inflater.inflate(R.layout.fragment_layout, 5 container, false); 6 //init other view 7 return root; 8 } Use onCreateView callback to set view !
  • 24. Fragment tips Toast.makeText(getActivity(), "Mission complete", Toast.LENGTH_LONG); Get context from activity Add to BackStack 1 FragmentManager fm = getFragmentManager(); 2 FragmentTransaction ft = fm.beginTransaction(); 3 ft.replace(R.id.fragment_container, fragment); 4 ft.addToBackStack(null);
  • 25. Import v4 or origin version 1 import android.support.v4.app.FragmentActivity; 2 import android.support.v4.app.FragmentManager; 3 import android.support.v4.app.FragmentTransaction; 4 import android.support.v4.app.Fragment; 1 import android.app.Activity; 2 import android.app.FragmentManager; 3 import android.app.FragmentTransaction; 4 import android.app.Fragment;
  • 26. Use new Fragment() or Fragment.newInstance()? 1 ChannelFragment fragment = new ChannelFragment(); 2 Bundle args = new Bundle(); 3 args.putInt(CHANNEL_PAGE_INDEX, index); 4 fragment.setArguments(args);
  • 27. 1 public static ChannelFragment newInstance(int index) { 2 ChannelFragment fragment = new ChannelFragment(); 3 Bundle args = new Bundle(); 4 args.putInt(CHANNEL_PAGE_INDEX, index); 5 fragment.setArguments(args); 6 return fragment; 7 } ChannelFragment.newInstance(index); Use static factory method for better!