Getting to Know Android App Structures: Activities, Fragments, and Broadcast Receivers

Benefits of Using Fragment

Fragment provides some key benefits in Android app development:

  1. Modularity: By dividing Activity into Fragment, you can manage each part of the interface separately, which makes it easier to develop and maintain code.
  2. Flexibility: Fragment allows the appearance of several parts of the interface in a single Activity, especially useful for devices with large screens such as tablets.
  3. Reusability: Fragment can be reused across a variety of Activity, reducing code repetition and improving development efficiency.
  4. Adaptability: Fragment easy interface customization for different screen sizes and device orientations.

Examples of Using Fragments in E-commerce Applications:

  • Fragment First: Displays the list of products on the left side of the screen.
  • Fragment Second: Displays product details on the right side of the screen.
  • On mobile, only one Fragment is displayed at a time, while on tablets, both Fragment can be displayed at the same time.

How Fragment Works

Fragment have a lifecycle similar to Activity, including methods such as onCreateView(), onStart(), and onDestroyView(). However, the life cycle is Fragment related to the Activity that displays it.

You can add Fragment to Activity by using FragmentManager and FragmentTransaction.

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =   fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, new MyFragment());
fragmentTransaction.commit();

Fragment cannot communicate with each other directly. Alternatively, communication is carried out through the Activity that houses the Fragment or by using an interface (interface) to transfer data.

3. Intent: Communication Mechanism Between Components

Intent is one of the key mechanisms in Android app development. This component is used to start an Activity, Service, or send Broadcast. In addition, Intent also allows communication between components within the same application or even between different applications.

Intent can be differentiated into:

  • Explicit: Calls a specific component (such as Activity or Service) by mentioning its class name directly.
  • Implicit: Specify the action you want to take or the type of data you want to process, and the Android system will select the most appropriate component.

Types of Intent

1. Explicit Intent

Explicit Intent is used when you know clearly which component you want to call, such as a specific Activity or Service in your application.

How It Works:

You specify the name of the component class you want to access. For example, starting a new Activity to display product details.

Intent intent   new Intent(this, ProductDetailActivity.class);
startActivity(intent);

Usage Example:

  • Start a new Activity to view the login form.
  • Start Service to download files in the background.

2. Intent Implicit

Intent Implicit is used when you want to ask the Android system to select the most appropriate component based on the actions or data types you specify.

How It Works:

You specify the actions (such as ACTION_VIEW) and data (such as URIs or MIME types) that you want to process. The Android system will look for a component that can handle the request.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(“https://www.example.com”));
startActivity(intent);

Usage Example:

  • Open the URL in the user’s default browser.
  • Share text or images to other apps (such as social media or email).
  • Select the app to open the PDF file.

The Difference Between Explicit and Implicit Intent

AspectsExplicit IntentImplicit Intent
PurposeCalls specific components in the application.Prompting the system to select the appropriate component.
UseUsed for internal navigation of the application.Used to interact with other applications or system components.
ExampleStart the product details activity.Open the URL in a browser or share content.

Latest Articles