Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Fragments
Search
Rafael Toledo
August 25, 2017
Programming
4
340
Fragments
Apresentada na Android Dev Conference 2017
Rafael Toledo
August 25, 2017
Tweet
Share
More Decks by Rafael Toledo
See All by Rafael Toledo
Gamedev com Kotlin Native
rafaeltoledo
0
81
Kotlin Multiplataforma: Compartilhando código entre Android e iOS
rafaeltoledo
0
290
Motion Layout
rafaeltoledo
1
130
Pipeline Android
rafaeltoledo
3
160
Android Architecture Components
rafaeltoledo
7
160
What's New in Kotlin 1.3
rafaeltoledo
0
150
An Overview of Multiplatform Kotlin
rafaeltoledo
2
120
Compartilhando Código com Kotlin Multiplataforma
rafaeltoledo
2
260
Android Assíncrono
rafaeltoledo
3
220
Other Decks in Programming
See All in Programming
チームの境界をブチ抜いていけ
tokai235
0
180
品質ワークショップをやってみた
nealle
0
280
ソフトウェア設計の実践的な考え方
masuda220
PRO
4
590
株式会社 Sun terras カンパニーデック
sunterras
0
320
Devoxx BE - Local Development in the AI Era
kdubois
0
130
他言語経験者が Golangci-lint を最初のコーディングメンターにした話 / How Golangci-lint Became My First Coding Mentor: A Story from a Polyglot Programmer
uma31
0
160
uniqueパッケージの内部実装を支えるweak pointerの話
magavel
0
1k
タスクの特性や不確実性に応じた最適な作業スタイルの選択(ペアプロ・モブプロ・ソロプロ)と実践 / Optimal Work Style Selection: Pair, Mob, or Solo Programming.
honyanya
3
170
Cursorハンズオン実践!
eltociear
2
1.1k
私達はmodernize packageに夢を見るか feat. go/analysis, go/ast / Go Conference 2025
kaorumuta
2
570
(Extension DC 2025) Actor境界を越える技術
teamhimeh
1
250
オープンソースソフトウェアへの解像度🔬
utam0k
15
2.9k
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
100
5.8k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Building a Scalable Design System with Sketch
lauravandoore
463
33k
For a Future-Friendly Web
brad_frost
180
9.9k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
GitHub's CSS Performance
jonrohan
1032
470k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
33
2.3k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.2k
How to train your dragon (web standard)
notwaldorf
97
6.3k
A Modern Web Designer's Workflow
chriscoyier
697
190k
Transcript
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