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

Accessing Resources through Class R

When an app is compiled, Android automatically generates an R class that contains references to all resources defined in the /res directory. You can access these resources through the R class in the program code.

Example of Accessing Strings:

String appName = getString(R.string.app_name);

Example of Accessing Images:

ImageView imageView = findViewById(R.id.my_image_view);
imageView.setImageResource(R.drawable.ic_launcher);

Example of Accessing a Layout:

setContentView(R.layout.activity_main);

Conclusion

By understanding the key components like Activity, Fragment, Intent, and Service, you can build more efficient, modular, and manageable Android apps. Each component has a specific role that allows the app to function properly, both in the foreground and in the background.

Additionally, components such as Broadcast Receiver and Content Provider allow your app to respond to system changes and share data with other apps.

By leveraging Application Manifest and Application Resources, you can define the structure of your application and manage assets such as strings, images, and layouts in a more organized way.

Latest Articles