programming with android: android fragments · android: managing fragments a fragment can get a...
TRANSCRIPT
![Page 1: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/1.jpg)
Programming with Android: Android Fragments
Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna
![Page 2: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/2.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets 2
Outline
Android for Tablets: Fragments Transactions
Android for Tablets: Fragments Lifecycle
Android for Tablets: Fragments Layout
Android for Tablets: Fragments Creation
Android for Tablets: Fragments Design Philosophy
Android for Tablets: A Case Study
Android for Tablets: Fragments Back State
![Page 3: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/3.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 3
Android: Application Case Study
Option1
Option2
LIST of Wireless Networks Detected
Statistics
Net0 -123dbm channel 2 WEP
Net1 -93dbm channel 1 NONE
Net2 -93dbm channel 2 WPA
Channel0
CH
n
CH
n
CH
n
CH
n
CH
n
CH
n
CH
n
Option Panel
Sub- Activity
Sub- Activity
![Page 4: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/4.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 4
Android: Fragments
Fragment à A portion of the user interface in an Activity.
Practically, a Fragment is a modular section of an Activity. DESIGN PHILOSOPHY Ø Structure an Activity as a collection of Fragments.
Ø Reuse a Fragment on different Activities …
Introduced from Android 3.0 (API Level 11)
![Page 5: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/5.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 5
Android: Fragments Design Philosophy
EXAMPLE: Using Fragments with Smartphone layout
![Page 6: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/6.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 6
Android: Fragments Design Philosophy
EXAMPLE: Using Fragments with Tablet layout
![Page 7: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/7.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 7
Android: Fragment Transactions
EXAMPLE: Using Fragments on Different Devices (Smartphone/Tab)
![Page 8: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/8.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 8
Android: Adding a Fragment to the UI
Specify layout properties for the Fragment as it were a View. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <fragment android:name="it.cs.android30.FragmentOne" android:id="@+id/f1" android:layout_width="wrap_content" android:layout_height="fill_parent" /> <fragment android:name="it.cs.android30.FragmentTwo" android:id="@+id/f2" android:layout_width="wrap_content" android:layout_height="fill_parent" /> </LinearLayout>
![Page 9: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/9.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 9
Android: Fragment Creation
To define a new Fragment à create a subclass of Fragment.
public class MyFragment extends Fragment { …}
Ø Has its own lifecycle (partially connected with the Activity lifecyle)
Ø Has its own layout (or may have)
Ø Can receive its own input events Ø Can be added or removed while the Activity is running.
PROPERTY of a Fragment:
![Page 10: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/10.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 10
Android: Fragment Lifecycle
Several callback methods to handle various stages of a Fragment lifecycle: onCreate() à called when creating the Fragment. onCreateView() à called when it is time for the Fragment to draw the user interface the first time.
![Page 11: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/11.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 11
Android: Fragment Lifecycle
onAttach() onCreate() onCreateView() onActivityCreated() onStart() onResume()
onPause() onStop() onDestroyView() onDestroy() onDetach()
CALLED when FRAGMENT is CREATED
CALLED when FRAGMENT is DESTROYED
![Page 12: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/12.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 12
Android: Fragment Creation
onCreateView() à must return the View associated to the UI of the Fragment (if any) …
public class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
return inflater.inflate(R.layout.example_fragment, container, false);
}
![Page 13: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/13.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 13
Android: Fragment Lifecycle
The lifecycle of the Activity in which the Fragment lives directly affects the lifecycle of the Fragment. onPause (Activity) à onPause (Fragment) onStart (Activity) à onStart (Fragment) onDestroy (Activity) à onDestroy (Fragment) Fragments have also extra lifecycle callbacks to enable runtime creation/destroy.
![Page 14: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/14.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 14
Android: Fragment Lifecycle
ACTIVITY]: onCreate() --- beforeSetContentView [FRAGMENT]: onAttach() [FRAGMENT]: onCreate() [FRAGMENT]: onCreateView() [ACTIVITY]: onCreate() --- after SetContentView [FRAGMENT]: onActivityCreated() [ACTIVITY]: onStart() [FRAGMENT]: onStart() [ACTIVITY]: onResume() [FRAGMENT]: onResume()
[FRAGMENT]: onPause() [ACTIVITY]: onPause() [FRAGMENT]: onStop() [ACTIVITY]: onStop() [FRAGMENT]: onDestroyView() [FRAGMENT]: onDestroy() [FRAGMENT]: onDetach() [ACTIVITY]: onDestroy()
![Page 15: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/15.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 15
Android: Managing Fragments
A Fragment can get a reference to the Activity …
Activity getActivity()
An Activity can get a reference to the Fragment …
ExampleFragment fragment=(ExampleFragment)
getFragmentManager().findFragmentById(R.id.example_fragment)
The FragmentManager manages the Fragment associated to the current Activity.
![Page 16: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/16.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 16
Android: Managing Fragments
In some cases, a Fragment must share an event with the Activity … how to do it? 1. Define a callback interface inside the Fragment
2. Require that the host Activity implements it
public interface OnArticleSelectedListener { public void onArticleSelected(Uri uri); …
}
![Page 17: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/17.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 17
Android: Fragment Transactions
Ø Fragments can be added/removed/replaced while the Activity is running …
Ø Each set of changes to the Activity is called a Transaction.
Ø Transaction can be saved in order to allow a user to navigate backward among Fragments when he clicks on the “Back” button.
![Page 18: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/18.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 18
Android: Fragment Transactions
FragmentManager man=getFragmentManager(); FragmentTransaction transaction=man.beginTransaction();
FragmentExample newFragment=new FragmentExample(); transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackState(null);
transaction.commit();
1. ACQUIRE an instance of the FRAGMENT MANAGER
2. CREATE new Fragment and Transaction
3. SAVE to backStack and COMMIT
![Page 19: Programming with Android: Android Fragments · Android: Managing Fragments A Fragment can get a reference to the Activity … Activity getActivity() An Activity can get a reference](https://reader030.vdocument.in/reader030/viewer/2022041016/5ec8d02d598f0f111b7110bc/html5/thumbnails/19.jpg)
Luca Bedogni, Marco Di Felice – Android for Tablets (c) Luca Bedogni 2012 19
Android: Fragment Transactions
Ø A Transaction is not performed till the commit …
Ø If addToBackStack() is not invoked à the Fragment is destroyed and it is not possible to navigate back.
Ø If addToBackStack() is invoked à the Fragment is stopped and it is possible to resume it when the user navigates back.
Ø popBackStack() à simulate a Back from the user.