Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
深入淺出談fragment
Search
fantasy
January 30, 2015
Programming
100
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
深入淺出談fragment
有基本知識來介紹frgament元件,到要如何使用fragment,以及需要注意的地方。
fantasy
January 30, 2015
More Decks by fantasy
See All by fantasy
Use multi Analytics and Crashlytic smothly
fantasy1022
0
63
Exploring RxJava2
fantasy1022
0
66
Other Decks in Programming
See All in Programming
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
6
2k
スマート反転とウェブアクセシビリティ
camiha
0
200
個人開発基盤をまるごとCloudflareに引っ越して爆速で総合的体験を向上させた話
tinykitten
0
170
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
310
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
370
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
3
530
go-spidermonkeyでAIエージェントのCode Modeを実装する
syumai
3
1.5k
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
240
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
6
1.7k
AIとGame Jamで、ゲームを完成させた話
takahirosaeki
0
120
『寄り添うラジオ』をAIで作る 体験価値から逆算した、会話しないUXと品質設計
theoriatec2024
3
160
巨大モノリシックアプリ モダン化大作戦
ktcryomm
0
950
Featured
See All Featured
Believing is Seeing
oripsolob
1
220
AI: The stuff that nobody shows you
jnunemaker
PRO
9
990
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
700
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
220
Optimizing for Happiness
mojombo
378
71k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
4 Signs Your Business is Dying
shpigford
187
23k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Claude Code のすすめ
schroneko
67
230k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
520
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
670
Transcript
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
None
None
None
None
None
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