Android App Architecture – Key Components and Structure - Textnotes

Android App Architecture – Key Components and Structure


Learn the fundamental structure of an Android application, including Manifest file, Resources, Layouts, Activities, Services, Content Providers, and Broadcast Receivers. Understand how each component contributes to the overall design of an Android app.

Android architecture is the foundation of Android app development. Understanding the core components will help you design scalable, efficient, and maintainable apps. This section provides a detailed explanation of the Android app structure and key components that play a significant role in the functioning of Android apps.

1. Android App Structure

Every Android app follows a certain structure, which includes key components that work together to provide the app's functionality.

1.1 AndroidManifest.xml

  1. The Manifest file is the heart of an Android app. It is an essential file that contains important metadata about the app.
  2. Key Functions:
  3. Declares App Components: Activities, Services, Broadcast Receivers, Content Providers.
  4. Defines Permissions: What features or data the app requires access to (e.g., camera, location).
  5. Specifies Application Theme and Configuration: UI theme, API level, and screen orientation.

Example:


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.MyApp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

1.2 Resources (res Folder)

  1. The resources folder contains non-code assets such as images, strings, layouts, and XML configuration files.
  2. drawable: Images and graphics (e.g., PNG, JPEG).
  3. layout: XML files defining the app’s UI.
  4. values: Strings, colors, dimensions, styles, and arrays (e.g., strings.xml, colors.xml).

Example:


<resources>
<string name="app_name">My Application</string>
<color name="colorPrimary">#3F51B5</color>
</resources>

1.3 Layouts

  1. Layouts define the structure of the user interface, including how views (UI elements like buttons, text fields, and images) are arranged on the screen.
  2. XML Layouts: Android uses XML to define the layout. You can use different layout types like LinearLayout, RelativeLayout, ConstraintLayout, etc.

Example:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>

2. Key Android Components

2.1 Activities

  1. Activity is the entry point of an Android app. Every screen in the app is represented by an Activity.
  2. Activity Lifecycle: Activities go through various stages, such as onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy().
  3. An app can have multiple activities, and navigation between them is typically handled by Intents.

Example:


public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
  1. Intent: Used to launch an Activity or interact with another component.

Example of starting an activity:


Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

2.2 Services

  1. Service is a background component that runs independently of any UI. Services do not have a user interface and are used to perform long-running operations like playing music, downloading files, etc.
  2. Types of Services:
  3. Started Service: Initiated by calling startService() and runs until explicitly stopped.
  4. Bound Service: Provides an interface for other components (like activities) to communicate with the service.

Example:


public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Perform background task here
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null; // No binding in this case
}
}
  1. Starting a Service:

Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);

2.3 Content Providers

  1. Content Providers allow sharing of data between applications. For example, your app can access contacts, images, or other apps' data using content providers.
  2. Key Functions: They encapsulate data access, handle permissions, and provide a standard interface for accessing data.

Example:


ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null
);
  1. Content URI: The content provider provides a URI for accessing data.

2.4 Broadcast Receivers

  1. Broadcast Receivers are used to listen for system-wide events or notifications, such as battery status, Wi-Fi status, or custom app events.
  2. A Broadcast Receiver listens for Broadcast Intents and handles them in a background process.

Example:


public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Handle the broadcast intent here
}
}
  1. Registering the Receiver: You can register receivers in the Manifest file or at runtime.
  2. Manifest Registration: To listen for system broadcasts like battery status, register the receiver in the AndroidManifest.xml file.

Example (Manifest registration):


<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter>
</receiver>

3. Summary of Key Components

  1. Activities: The main UI components of the app that handle user interactions.
  2. Services: Used for background tasks such as playing music or fetching data.
  3. Content Providers: Provide a mechanism for sharing data between applications.
  4. Broadcast Receivers: Respond to system-wide or app-specific events and messages.