Fragments Without User Interfaces

In most circumstances, Fragments are used to encapsulate modular components of the UI; however, you can also create a Fragment without a UI to provide background behavior that persists across Activity restarts. This is particularly well suited to background tasks that regularly touch the UI or where it’s important to maintain state across Activity restarts … Read more

Interfacing Between Fragments and Activities in Android

Use the getActivity method within any Fragment to return a reference to the Activity within which it’s embedded. This is particularly useful for fi nding the current Context, accessing other Fragments using the Fragment Manager, and finding Views within the Activity’s View hierarchy. TextView textView = (TextView)getActivity().findViewById(R.id.textview); Although it’s possible for Fragments to communicate directly … Read more

Animating Fragment Transactions in Android

To apply one of the default transition animations, use the setTransition method on any Fragment Transaction, passing in one of the FragmentTransaction.TRANSIT_FRAGMENT_* constants. You can also apply custom animations to Fragment Transactions by using the setCustom Animations method. This method accepts two animation XML resources: one for Fragments that are being added to the layout … Read more

Fragment Manager to Find Fragments in Android

To find Fragments within your Activity, use the Fragment Manager’s findFragmentById method. If you have added your Fragment to the Activity layout in XML, you can use the Fragment’s resource identifier: If you’ve added a Fragment using a Fragment Transaction, you should specify the resource identifier of the container View to which you added the … Read more

Fragment Manager in Android

Each Activity includes a Fragment Manager to manage the Fragments it contains. You can access the Fragment Manager using the getFragmentManager method: The Fragment Manager provides the methods used to access the Fragments currently added to the Activity, and to perform Fragment Transaction to add, remove, and replace Fragments Adding Fragments to Activities Adding Fragments … Read more

Optimizing Layouts in Android

Inflating layouts is an expensive process; each additional nested layout and included View directly impacts on the performance and responsiveness of your application Redundant Layout Containers Are Redundant A Linear Layout within a Frame Layout, both of which are set to MATCH_PARENT, does nothing but add extra time to inflate. Look for redundant layouts, particularly … Read more

Layouts in Android

Layout Managers (or simply layouts) are extensions of the ViewGroup class and are used to position child Views within your UI. Layouts can be nested, letting you create arbitrarily complex UIs using a combination of layouts. The Android SDK includes a number of layout classes. You can use these, modify them, or create your own … Read more