Skip to main content

Android - Activities

If you worked with programming language like C, C++ or Java. That means you know that program is start with the main() function. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity as shown in Activity life cycle diagram.




The Activity class defines the following call backs i.e. events. You don't need to implement all the callbacks methods. You only need to understand all.

  1. onCreate()
  2. onStart()
  3. onResume()
  4. onPause()
  5. onStop()
  6. onDestroy()
  7. onRestart()
Following is the code of the modified main activity file src/com.example.sample/MainActivity.java. The Log.d() method has been used to generate log messages.

package com.example.sample;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity {
   String msg = "Android : ";
   
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.d(msg, "The onCreate() event");
   }

   @Override
   protected void onStart() {
      super.onStart();
      Log.d(msg, "The onStart() event");
   }

   @Override
   protected void onResume() {
      super.onResume();
      Log.d(msg, "The onResume() event");
   }

   @Override
   protected void onPause() {
      super.onPause();
      Log.d(msg, "The onPause() event");
   }

   @Override
   protected void onStop() {
      super.onStop();
      Log.d(msg, "The onStop() event");
   }

   @Override
   public void onDestroy() {
      super.onDestroy();
      Log.d(msg, "The onDestroy() event");
   }
}

An activity class loads all the UI component using the XML file available in res/layout folder of the project. Following statement loads UI components from res/layout/activity_main.xml file:

setContentView(R.layout.activity_main);

An application can have one or more activities without any restrictions. Every activity you define for your application must be declared in your AndroidManifest.xml file and the main activity for your app must be declared in the manifest with an <intent-filter> that includes the MAIN action and LAUNCHER category as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <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>

Comments

Popular posts from this blog

Android - Broadcast Receivers

Broadcast Receivers   simply respond to broadcast messages from other applications or from the system itself. These messages are also called as events or intents. There are following two important steps to make BroadcastReceiver works for the system broadcasted intents − ·         Creating the Broadcast Receiver. ·         Registering Broadcast Receiver A broadcast receiver is implemented as a subclass of   BroadcastReceiver   class and overriding the onReceive() method where each message is received as a   Intent   object parameter. Registering Broadcast Receiver An application listens for specific broadcast intents by registering a broadcast receiver in  AndroidManifest.xml  file. Consider we are going to register  MyReceiver  for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot pr...

Android - Notifications

A   notification   is a message you can display to the user outside of your application. I t first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time. Android   Toast   class is used for alerts but problem is that these alerts are flashes on the screen for a few seconds and then disappears. To see the details of the notification, you will have to select the icon which will display notification drawer having detail about the notification. While working with emulator with virtual device, you will have to click and drag down the status bar to expand it which will give you details. Create and Send Notifications You can create Notification very simply and easy way. Follow the steps to create Notification in your application. Step 1 - Create Notifica...

Android - How to Customize a Toast in android

Custom Toast In Android : Hello, Today i will teach you about message toast used in android. You can create it with your own design or own functionality. You can also change its position where to show. Watch this video to create custom design of toast in android. If you like my post then please share and like it. Also like video on YouTube. If you have any query you can ask me by commenting on post or video. Thank you.