Blog Infos
Author
Published
Topics
,
Published
  1. App Components
  2. Intents
  3. App Entry Points
App Components

 

 

Activities
Activity Lifecycles

Android Docs

  • onCreate(): This callback is invoked when the system creates your activity. Most of the initialization logic, which should occur only once during an Activity’s lifespan, should be placed here (like creating views or binding data).
  • onStart(): This callback is invoked after calling the onCreate() method as the activity becomes visible to the user. This may happen more than once if you switch between multiple Activities or applications.
  • onResume(): This means the activity is ready to come to the foreground and interact with users.
  • onPause(): This means the activity is no longer in the foreground, and may still be partially visible (for instance, if the user is in multi-window mode). In most cases, it indicates the user is leaving the activity, and the activity will enter the next state.
  • onStop(): This callback is invoked 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 invoked before an activity is destroyed. The system invokes this callback when the activity is finishing or the system is temporarily destroying the activity due to a configuration change. This callback can be utilized when you need to release or dismiss all remaining resources and allow the Garbage Collector to withdraw all allocated memories.
Creating an Activity
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 on the App Manifest as seen in the following example:

<manifest ... >
<application ... >
<activity android:name=".MainActivity" />
...
</application ... >
...
</manifest >
Services
Service Lifecycles
  • startService: Another component can run a service by calling startService(). This service will run in the background and another component can also stop the service by calling stopService().
  • bindService: Another component or a client can run a service by calling bindService(). The bindService() function provides a IBinder interface, which allows the client to communicate with the service consistently. This service will run in the background. Another component or client can also cancel the connection by calling unbindService.

Android Docs

Creating a Service
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
}
}
<manifest ... >
<application ... >
<service android:name=".MyService" />
...
</application ... >
...
</manifest >

Job Offers

Job Offers


    Mobile Engineer

    OLX Group
    Remote, Portugal, Spain, Romania, Poland
    • Full Time
    apply now

    Kotlin Multiplatform Mobile Developer

    Touchlab
    Remote
    • Full Time
    apply now

    Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

, ,

From Scoped Storage to Photo Picker: Everything to know about Storage

Persistence is a core element of every mobile app. Android provides different APIs to access or expose files with different tradeoffs.
Watch Video

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocate
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer ad ...
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocat ...
Google

Jobs

Broadcast Receiver
Creating a Broadcast Receiver
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 on 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>
Content Providers

Android Docs

Intents

Use Cases of Intents
  • Starting an activity: You can launch a new activity by passing an intent to the startActivity() method. The intent defines the activity’s behavior and delivers the necessary data that should be used in a new activity.
  • Starting a service: You can run a new service by passing an intent to the startService() method. The intent defines the service’s behavior and delivers the necessary data that should be used in a new service.
Intent Types
  • Explicit Intents: Explicit intent includes specified information, which targets an application’s package name or a fully-qualified component class name. For example, you can start an Activity/Service or send a message to a Broadcast Receiver with an Intent that includes an explicit target class or package information.
  • Implicit Intents: Implicit Intent does not include specified target information, but instead declares a general action to perform. For example, if you want to show the user images in a gallery or open a URL on a web browser, you can use an implicit intent to request action to the Android system. Then, the Android system searches all installed applications for intent filters and compares the appropriate component to start the implicit intent. If the Android system finds an appropriate component, it will show you the available list of components, but if not, you can’t perform the implicit intent.

 

Android Docs

 

App Entry Points

 

 

App Shortcuts

Android Docs

  • Static shortcuts: Static shortcuts provide links to consistent actions within your app and perform static tasks, which are not dependent on user context. For example: Displaying recent messages, writing a post, or searching for a keyword.
  • Dynamic shortcuts: Dynamic shortcuts provide links to specific actions, which are based on user context within your 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) allow you to pin a shortcut onto a supported launcher. You can run a specific task by clicking on the pinned shortcuts on the home screen.
Creating Static Shortcuts
<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 on the App Manifest as seen in the example below:

<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>
Conclusion

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
If you think your app is doing some useful work that might be useful…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu