Presentation is loading. Please wait.

Presentation is loading. Please wait.

Widgets & Fragments Kalin Kadiev Astea Solutions AD.

Similar presentations


Presentation on theme: "Widgets & Fragments Kalin Kadiev Astea Solutions AD."— Presentation transcript:

1 Widgets & Fragments Kalin Kadiev Astea Solutions AD

2

3 App widgets

4 App widgets Not just widgets, they are app widgets!
Miniature, embeddable application views. Published by app widget providers, held by app widget hosts. Configured by app widget configuration activity.

5 App widget practices Update as infrequently as possible!
Don't wake the device unless you really need it. Don't use more than 4x4 cells for your widget. NB: you can use a small subset of the available views. You can avoid waking the device while asleep by using AlarmManager. More info at and

6 Remote Views Describes a view hierarchy that can be displayed in another process. Provides layout resource, that will be inflated Provides some basic operations for modifying the content of the inflated hierarchy.

7 Fragments A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity. Has its own lifecycle; Receives its own input events; Can be added or removed while the activity is running.

8 Fragments

9 Fragment Lifecycle fragment is added onAttach() onCreate()
onCreateView() onResume() onStart() onActivityCreated() fragment is active Fragment is removed Fragment is added to the back stack, then removed Fragment is restored from the back stack onAttach(Activity) called once the fragment is associated with its activity. onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreaate. onStart() makes the fragment visible to the user (based on its containing activity being started). onResume() makes the fragment interacting with the user (based on its containing activity being resumed). As a fragment is no longer being used, it goes through a reverse series of callbacks: onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity. onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity. onDestroyView() allows the fragment to clean up resources associated with its View. onDestroy() called to do final cleanup of the fragment's state. onDetach() called immediately prior to the fragment no longer being associated with its activity. onPause() onStop() onDestroyView() fragment is destroyed onDetach() onDestroy()

10 Coordinating lifecycles
Each activity callback invokes a corresponding fragment callback; When the fragment is added to the activity, onAttach() is called; When the fragment is removed from the activity, onDetach() is called; When the activity's onCreate() returns, onActivityCreated() is called.

11 Managing fragments Two words: use FragmentManager
Get existing fragments: findFragmentById() findFragmentByTag() Manage back stack: popBackStack() Manage fragments: use transactions Some words about fragment transactions: Create a new transaction via FragmentManager.beginTransaction() Create a new instance of your fragment Use FragmentTransaction.add/replace/remove (passing the fragment instance and the id of the container in the view hierarchy) Optionally, use FragmentTransaction.addToBackStack() Use FragmentTransaction.commit() Voila!

12 Fragments for all Why use fragments? What happened to TabActivity?
Large screens handling More powerful than tab layouts. What happened to TabActivity? So, how to make tabs in x? (96% of the current devices) Compatibility library!

13 Q&A + Feedback Questions? Feedback section: Did you hear well?
Was there anything you didn’t understand? What would you like changed in our next lecture?

14 Recommended resources
Fragment documentation Fragments for All Application widgets App widget design guidelines RemoteViews


Download ppt "Widgets & Fragments Kalin Kadiev Astea Solutions AD."

Similar presentations


Ads by Google