Xamarin Activities and Fragments
Xamarin provides two fundamental building blocks for building Android applications - Activities and Fragments. Activities represent an individual screen in a user interface, while Fragment represents a reusable portion of the user interface within an activity. In this guide, we will be exploring activities and fragments in more detail.
Syntax
Here's an example of an activity's basic syntax:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Here's an example of a fragment's basic syntax:
class ExampleFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_example, container, false)
}
}
Example
To better understand the concept of activities and fragments, let's go over a simple example. Assume that we are building an application that consists of two screens - a home screen and a user profile screen. We can represent each screen using an activity.
The home screen activity will have a search bar and a list of items representing different user profiles. When a user clicks on a profile, the application should navigate to the user profile activity.
Inside the user profile activity, we can use a fragment to display the user's information, such as their picture, name, and bio.
Output
The output of this example application would be a fully functional Android application with two screens. The user can search for, select, and view different user profiles that contain their full name, bio, and profile picture.
Explanation
Activities are the primary building blocks of an Android application, representing individual screens or windows. Every activity has a unique life cycle, which is managed by the Android system. An activity can contain fragments, which provide a way to reuse portions of the user interface across different activities.
Fragments are self-contained user interface components that represent a reusable portion of a user interface. They have their own lifecycle, which is distinct from the lifecycle of the activity that hosts them. Fragments can be used to represent static content or dynamic content that changes based on user interaction.
Use
Activities and fragments are used in Android development to create modular, reusable, and responsive user interfaces. By using activities and fragments, developers can create complex, multi-screen applications that are both user-friendly and easy to maintain.
Important Points
- Activities represent individual screens or windows in an Android application.
- Activities have a unique life cycle, which is managed by the Android system.
- Fragments are self-contained user interface components that can be reused across different activities.
- Fragments have their own lifecycle, which is distinct from the lifecycle of the activity that hosts them.
- Fragments can be used to display either static or dynamic content.
Summary
In this guide, we have explored the fundamental building blocks of an Android application, activities and fragments. Activities represent individual screens or windows, while fragments represent reusable portions of the user interface within an activity. Fragments can be used to display either static or dynamic content, making them incredibly versatile and useful for modern Android development.