Android Developer Route 2023 - Part 2

Android roadmap
Android Developer Route 2023 - Part 1

Android Developer Route 2023 - Part 2

Android Developer Route 2023 - Part 3

Android Developer Route 2023 - Part 4

2023 Android Developer Route - Part 2

In the previous article, we discussed important elements of the Android architecture, including the main Android language, operating system, Android platform, and App Manifest.
In Part 2, we'll cover the next three parts of the Android roadmap:

  1. application components
  2. intention
  3. application entry point

application components

Android components
In Android development, application components are like entry points that allow the system and users to interact with your application. Each component has a unique function and lifecycle that determine how it is created and destroyed.

Let's discuss each component:

Activity

Activity is an independent and reusable component that interacts with the user by providing UI-related resources. All Android applications must have at least one Activity to enter the application and interact with the user.

Activity Lifecycle
All activities have their own life cycle, which is an important concept for managing activities and resources. The Activity class provides a core set of callback methods for notifying an activity that its lifecycle state has changed.

The callback methods will be called in the order of the lifecycle as shown in the following diagram:
Activity Lifecycle
You can declare how your activity behaves and use the lifecycle callback methods to efficiently manage your resources. In this section, we discuss the following six core callback methods:

  • onCreate(): This callback is called when the system creates your activity. Most initialization logic that should happen only once during the Activity's lifecycle should go here (such as creating views or binding data).
  • onStart(): This callback is called after the method is called when the activity becomes visible to the user. This may happen more than once if you switch between multiple Activities or Applications.
  • onResume(): This means that the activity is ready to come to the foreground and interact with the user.
  • onPause(): This means that the activity is no longer in the foreground, and may still be partially visible (for example, if the user is in multi-window mode). In most cases it indicates that the user is leaving the activity and the activity will go to the next state.
  • onStop(): This callback is called when the activity is no longer visible to the user. This may happen more than once if you switch between multiple Activities or Applications.
  • onDestroy(): This callback is called before the activity is destroyed. This callback is called by the system when the activity ends or when the system temporarily destroys the activity due to a configuration change. Use this callback when you need to free or close any remaining resources and allow the garbage collector to reclaim all allocated memory.

Garbage collection: https://en.wikipedia.org/wiki/Garbage-first_collector
For more details, have a look at the Activity Lifecycle.
Lifecycle: https://developer.android.com/guide/components/activities/activity-lifecycle

Creating an Activity
To create an activity, you must create a class as a subclass of the Activity class. In modern Android development, the Jetpack library provides high-level Activity classes such as AppCompatActivity, FragmentActivity, and ComponentActivity to support compatibility with Themes, Fragments, etc. You can create a basic activity with the following code:

class MainActivity : AppCompatActivity() {
    
    

    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

For your app to use Activities, you must declare them in the App Manifest, as the following example shows:

<manifest ... >
  <application ... >
      <activity android:name=".MainActivity" />
      ...
  </application ... >
  ...
</manifest >

Service

A service is an entry point designed to perform functions for remote processes and run long-running operations in the background, such as a music player or a Youtube video player.

Service Life Cycle
Services have their own dedicated life cycle and provide two types to tell the system how to start and manage the service:

  • startService: Another component can call startService()to run the service. The service will run in the background, and another component can also call stopService()to stop the service.
  • bindService: Another component or client can call bindService()to run the service. This bindService()function provides an IBinder interface that allows clients to communicate consistently with the service. The service will run in the background. Another component or client can also unbindServicecancel the connection by calling.
    As shown in the diagram below, the lifecycle of a service depends on how it was created:
    Service Lifecycle
    according to the Android docs, the diagram above " separates bindService()created services from startService()created services", but they may allow clients to bind to them no matter how the services are started .

Creating a Service
To create a service, you must create a class as a subclass of the Service class, as shown in the following example:

class MyService : Service() {
    
    

    private var binder: IBinder? = null

    override fun onCreate() {
    
    
        // The service is being created
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    
    
        return super.onStartCommand(
            intent,
            flags,
            startId
        ) // indicates how to behave if the service is killed
    }

    override fun onBind(intent: Intent?): IBinder? {
    
    
        // A client is binding to the service with bindService()
        return binder
    }

    override fun onDestroy() {
    
    
        // The service is no longer used and is being destroyed
    }
}

Next, for your app to be able to use the service, you must declare it in the App Manifest:

<manifest ... >
  <application ... >
      <service android:name=".MyService" />
      ...
  </application ... >
  ...
</manifest >

Service of Android components: https://developer.android.com/reference/android/app/Service

BroadcastReceiver

A Broadcast receiver is a registerable listener that listens for broadcast messages from the Android system and other Android applications. According to the Android documentation, broadcasts are used to send messages across applications and outside of the normal user flow, such as when the system starts up or the device starts charging.

Unlike activities and services, broadcast receivers do not have a dedicated lifecycle. Instead, it will listen for all assigned event messages until unregistered.

Create a BroadcastReceiver

To create a broadcast receiver, you must create a class as a subclass of the broadcast receiver class, as shown in the following example:

class MyBroadcastReceiver : BroadcastReceiver() {
    
    

    override fun onReceive(context: Context, intent: Intent) {
    
    
        // do something
    }
}

Next, for your app to be able to use the service, you must declare it in the App Manifest:

<receiver android:name=".MyBroadcastReceiver"  android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
    </intent-filter>
</receiver>

ContentProvider

Content providers manage how your app's data is accessed and shared with other apps. According to the Android documentation, content providers enable your application to share any type of persistent data, whether stored on the file system, SQLite database, Jetpack Room, or on the network.

Content providers protect data by requiring specific permissions. If the requesting application does not have the required permissions, it cannot query the content provider's data.
contentProvider

ContentProvider: https://developer.android.com/reference/android/content/ContentProvider

Intent

Intent
Intent is an abstract description of an action to be performed later. For example, it allows you to trigger the entry point of an application component or send a message to a broadcast receiver.

Use Cases for Intents

  • Start Activity : You can start a new activity by passing an intent to startActivity()the method. Intents define the behavior of an activity and provide the necessary data that should be used in the new activity.
  • Start Service : You can run a new service by passing an intent to startService()the method. Intents define the behavior of the service and provide the necessary data that should be used in the new service.
  • Pass message to BroadcastReceiver : You can pass message to broadcast receiver by passing intent to sendBroadcast()or method. sendOrderedBroadcast()You can pass intents from other application components to your application or other applications as broadcast messages.

Intent Types
There are two types of intents:

  • Explicit Intent: Explicit intent includes specified information, they point to an application's package name or fully qualified component class name. For example, you can use intents that include explicit target class or package information to start an Activity/Service or send a message to a Broadcast Receiver.
  • Implicit Intent: An implicit intent does not include specified target information, but instead declares a general operation to be performed. For example, if you want to display an image to the user in a gallery or open a URL in a web browser, you can use an implicit intent to request the Android system to perform an action. The Android system then searches all installed applications for intent filters and compares the appropriate component to launch the implicit intent. If the Android system finds an appropriate component, it will display a list of available components, but if it can't find one, you can't execute an implicit intent.

Intent

More about Intent: https://developer.android.com/guide/components/intents-filters

App Entry Points

App Entry Points
There are two basic application entry points in Android - Activities and Application Shortcuts.

Activity state changes: https://developer.android.com/guide/components/activities/state-changes
task stack and rollback stack: https://developer.android.com/guide/components/activities/tasks-and -back-stack
parcelables and bundles: https://developer.android.com/guide/components/activities/parcelables-and-bundles

Application Shortcuts
Application shortcuts allow you to initiate specific actions within an application. You can display the shortcut by long pressing the application icon, or start the task by clicking the item in the list, as shown in the picture below: There
app shortcut
are three types of shortcuts:

  • Static shortcuts: Static shortcuts provide links to consistent actions within the app and perform static tasks that are not dependent on user context. For example: display recent messages, write a post, or search for keywords.
  • Dynamic shortcuts: Dynamic shortcuts provide links to specific actions based on user context within the app. For example: sending a message to a specific person or navigating to a specific location.
  • Pinned Shortcuts: Pinned Shortcuts (supported on Android 8.0 and higher) allows you to pin shortcuts to supported launchers. You can run specific tasks by clicking shortcuts pinned on the home screen.
    We discussed three types of shortcuts. In the next section, you'll describe how to create static shortcuts.

Creating a Static Shortcut
To create a static shortcut, first create a new resource file res/xml/shortcuts.xml:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <shortcut
    android:shortcutId="message"
    android:enabled="true"
    android:icon="@drawable/ic_message"
    android:shortcutShortLabel="@string/short_label"
    android:shortcutLongLabel="@string/long_label"
    android:shortcutDisabledMessage="@string/message_shortcut_disabled">
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.example.myapplication"
      android:targetClass="com.example.myapplication.ComposeActivity" />
    <categories android:name="android.shortcut.conversation" />
    <capability-binding android:key="actions.intent.CREATE_MESSAGE" />
  </shortcut>
</shortcuts>

Next, for your app to be able to display the shortcut, you must declare it in your app manifest, as in the following example:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="io.getstream.example">
  <application ... >
    <activity android:name="Main">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      
      <meta-data android:name="android.app.shortcuts"
                 android:resource="@xml/shortcuts" /> 
    </activity>
  </application>
</manifest>

in conclusion

This section describes the important parts of App Components, Life Cycle, and App Entry Point so that you can better understand how Android Components and their Life Cycle work.

Guess you like

Origin blog.csdn.net/u011897062/article/details/130681674