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
Fragments
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Rafael Toledo
August 25, 2017
Programming
360
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Fragments
Apresentada na Android Dev Conference 2017
Rafael Toledo
August 25, 2017
Transcript
76 slides
FRAGMENTS Rafael Toledo @_rafaeltoledo
None
FRAGMENTS
None
POR QUE USAR FRAGMENTS?
POR QUE NÃO USAR FRAGMENTS?
LIFECYCLE
LIFECYCLE
LIFECYCLE
LIFECYCLE
LIFECYCLE
LIFECYCLE [CRIES IN ANDROIDISH]
WTF??? • IllegalStateException • getActivity() == null • executePendingTransactions() •
<fragment>
WTF???
EM 2017, VALE A PENA USAR FRAGMENTS?
EM 2017, VALE A PENA USAR FRAGMENTS?
DIFERENTES LAYOUTS, DIFERENTES TELAS
DIFERENTES TAMANHOS DE TELA
DIFERENTES TAMANHOS DE TELA Nexus One!
Quem usa tablet hoje em dia???
DIFERENTES TAMANHOS DE TELA Moto C (480x854) Samsung Galaxy S8+
(1440x2960)
DIFERENTES TAMANHOS DE TELA
Quem usa app em landscape???
Quem usa app em landscape??? EU!
ENCAPSULAMENTO DA NAVEGAÇÃO
ENCAPSULAMENTO DE NAVEGAÇÃO
ENCAPSULAMENTO DE NAVEGAÇÃO getSupportFragmentManager() .beginTransaction() .add(R.id.container, new Step1Fragment()) .addToBackStack(null) .commit();
// Back button = fragmentManager.popBackStack()
ENCAPSULAMENTO DE NAVEGAÇÃO getSupportFragmentManager() .beginTransaction() .add(R.id.container, new Step1Fragment()) .addToBackStack(null) .commit();
BACKSTACK? !=
BACKSTACK? Volta para a home
BACKSTACK?
BACKSTACK? Volta para a home?
BACKSTACK? Toolbar na Activity Fragment com conteúdo
BACKSTACK getSupportFragmentManager() .beginTransaction() .add(R.id.container, new Step1Fragment()) .addToBackStack(null) // ??? .commit();
BACKSTACK getSupportFragmentManager() .beginTransaction() .add(R.id.container, new Step1Fragment()) .addToBackStack("Cart") .commit();
BACKSTACK getSupportFragmentManager() .beginTransaction() .add(R.id.container, new Step1Fragment()) .addToBackStack("Cart") .commit(); fragmentManager.popBackStack("Cart"); fragmentManager.popBackStack("Cart",
FragmentManager.POP_BACK_STACK_INCLUSIVE);
STATEFUL DIALOGS
POR QUE EU PRECISO DE DIALOGS COM ESTADO?
MUDANÇAS DE CONFIGURAÇÃO
EVITANDO INTERAÇÕES DEAD-END
CUIDADO COM O DIALOG INFINITO
COMPONENTIZAÇÃO DA UI
COMPONENTIZAÇÃO DA UI Fragments como uma porção da Activity View
Fragment View Fragment Fragment Activity Fragment Activity
// Dentro do Fragment ((MainActivity) getActivity()).method(); COMPONENTIZAÇÃO DA UI -
DON'T!
// Dentro do Fragment ((MainActivity) getActivity()).method(); COMPONENTIZAÇÃO DA UI -
DON'T!
// Dentro da View ((MainActivity) context).method(); COMPONENTIZAÇÃO DA UI -
DON'T!
// Dentro da View ((MainActivity) context).method(); COMPONENTIZAÇÃO DA UI -
DON'T!
OLHA O REÚSO...
FALANDO EM VIEWS...
CUSTOM VIEWS x FRAGMENTS • Micro interações isoladas? Custom Views!
• Interação complexa, lida com rede, hardware, intents? Fragments! • View Pager? •Com interação rica (é uma tela de fato?) - Fragments! •Conteúdo simples (carrossel, vitrine, cards?) - Custom Views!
VOLTANDO...
COMO TROCAR DADOS ENTRE FRAGMENTS?
COMO TROCAR DADOS ENTRE FRAGMENTS? • EventBus?
COMO TROCAR DADOS ENTRE FRAGMENTS? • EventBus?
COMO TROCAR DADOS ENTRE FRAGMENTS? • EventBus? • Através da
Activity, implementando um listener
COMO TROCAR DADOS ENTRE FRAGMENTS? • EventBus? • Através da
Activity, implementando um listener • Architecture Components!
COMO TROCAR DADOS ENTRE FRAGMENTS? • EventBus? • Através da
Activity, implementando um listener • Architecture Components!
// No Fragment viewModel = ViewModelProviders.of(this) .get(MyViewModel.class); COMO TROCAR DADOS
ENTRE FRAGMENTS?
// No Fragment viewModel = ViewModelProviders.of(getActivity()) .get(MyViewModel.class); COMO TROCAR DADOS
ENTRE FRAGMENTS?
// No Fragment viewModel = ViewModelProviders.of(getActivity()) .get(MyViewModel.class); COMO TROCAR DADOS
ENTRE FRAGMENTS?
INICIALIZAÇÃO DE COMPONENTES
<fragment android:name="com.google.android.gms.maps.MapFragment" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" /> INICIALIZAÇÃO DE COMPONENTES
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); INICIALIZAÇÃO DE COMPONENTES
MapView mapView; @Override public void onCreate(Bundle savedInstanceState) { ... mapView
= findViewById(R.id.map); } INICIALIZAÇÃO DE COMPONENTES
@Override public void onStart() { super.onStart(); mapView.onStart(); } @Override public
void onStop() { mapView.onStop(); super.onStop(); } INICIALIZAÇÃO DE COMPONENTES
@Override public void onStart() { super.onStart(); mapView.onStart(); } @Override public
void onStop() { mapView.onStop(); super.onStop(); } INICIALIZAÇÃO DE COMPONENTES Boilerplate?
<fragment android:name="com.google.android.gms.maps.MapFragment" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" app:apiKey="1234567890abcdef" /> INICIALIZAÇÃO DE COMPONENTES
<fragment android:name="com.google.android.gms.maps.MapFragment" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" app:apiKey="1234567890abcdef" /> INICIALIZAÇÃO DE COMPONENTES
@Override public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
... } INICIALIZAÇÃO DE COMPONENTES
@Override public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
... } INICIALIZAÇÃO DE COMPONENTES
@Override public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
... } INICIALIZAÇÃO DE COMPONENTES
DICAS FINAIS • Sempre guarde estado dos seus Fragments! •
setRetainInstance(true) não deve ser usado para Fragments com Views! (Architecture Components usa!)
QUE TAL DAR UMA SEGUNDA CHANCE AOS FRAGMENTS?
Fragment Tricks youtube.com/watch?v=eUG3VWnXFtg What the Fragment? youtube.com/watch?v=k3IT-IJ0J98 Documentação developer.android.com/guide/components/fragments.html LINKS
Fragment Tricks youtube.com/watch?v=eUG3VWnXFtg What the Fragment? youtube.com/watch?v=k3IT-IJ0J98 Documentação developer.android.com/guide/components/fragments.html LINKS
Centro Av. Presidente Wilson, 231 - 29º andar (21) 2240-2030
Cidade Monções Av. Nações Unidas, 11.541 - 3º andar (11) 4119-0449 Savassi Av. Getúlio Vargas, 671 Sala 800 - 8º andar (31) 3360-8900 www.concrete.com.br speakerdeck.com/rafaeltoledo github.com/rafaeltoledo twitter.com/_rafaeltoledo
More Decks by Rafael Toledo
See All by Rafael Toledo
Gamedev com Kotlin Native
rafaeltoledo
0
130
Kotlin Multiplataforma: Compartilhando código entre Android e iOS
rafaeltoledo
0
330
Motion Layout
rafaeltoledo
1
200
Pipeline Android
rafaeltoledo
3
220
Android Architecture Components
rafaeltoledo
7
240
What's New in Kotlin 1.3
rafaeltoledo
0
190
An Overview of Multiplatform Kotlin
rafaeltoledo
2
190
Compartilhando Código com Kotlin Multiplataforma
rafaeltoledo
2
320
Android Assíncrono
rafaeltoledo
3
260
Other Decks in Programming
See All in Programming
眠らない開発チームの作り方
biwakonbu
0
140
Crash Auto Pipelines
veronikapj
1
450
Skills Are the New npm [AAIF Bengaluru, Nov 2026]
shivamsaraswat
0
130
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
170
protojson における文字化けの扱い
kazuki1023
0
470
Enforcing Your Architecture with Unit Tests in the AI Era (Swift Connection X)
steliosf
PRO
1
200
Beyond Chatbots: Agentic UI with Open Standards @W-JAX 2026, München
manfredsteyer
PRO
0
130
Secure Your Coding Agent Like It’s Malware (v1.5) 🇬🇧 @Code.talks 2026
richargh
0
210
エージェントの管理もエージェントに – herdr-core と自作バックログで作る司令塔 –
nearme_tech
PRO
2
150
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
210
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
440
テストだけで見つかる? Rubyでつくるメソッド発掘器
izawa
0
160
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Producing Creativity
orderedlist
PRO
348
41k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
520
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
850
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
3
3.9k
My Coaching Mixtape
mlcsv
0
400
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
360
Test your architecture with Archunit
thirion
2
2.5k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
1.1k