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.
- onCreate()
- onStart()
- onResume()
- onPause()
- onStop()
- onDestroy()
- onRestart()
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
Post a Comment