Activity
The Activity class is base class for activities. Main purpose of activity is to create window and interact with the user. In the simple case, a window is created using setContentView(view) , where the view can be created programmatically or loaded from a layout resource. In more complex way, you will use fragments.
While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with R.attr.windowIsFloating set), Multi-Window mode or embedded into other windows.
add new activity
If you want to add a new activity to your application, you need to follow the below steps.
1. Prepare view for activity as resource in folder res/layout/, for example activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btDemo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDemoClick"
android:text="demo button" />
</LinearLayout>
The LinearLayout allows you to lay out UI elements horizontally or vertically. In this example it contains only one button.
There are many other useful layouts like FrameLayout, ConstraintLayout, etc.
2. Create new activity.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// specify resource of view
setContentView(R.layout.activity_main)
}
// method that will be used as button onClick handler.
fun onDemoClick(v: View?){
Toast.makeText(this, "Demo button was pressed", Toast.LENGTH_SHORT).show()
}
}
For backward compatibility in this example the AppCompatActivity is used instead Activity class.
3. Specify activity in the AndroidManifest.xml file.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.darkraha.demo_001_activity">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
In our case, we register activity as entrypoint to app. When user run app, he will see this activity. For regular activities you don't need the intent-filter node.
If all the activities are in one package, you can specify the package in the manifest node and then in the activity node, specify only the name of the activity class. In other cases you must specify full class name with package.
lifecycle
When you override method, you must call super method also.
method | description |
---|---|
onCreate(bundle) | Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart(). |
onRestart() | Called after your activity has been stopped, prior to it being started again. Always followed by onStart(). |
onStart() | Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden. |
onResume() |
Called when the activity will start interacting with the user. At this point your activity is at the top of its activity stack, with user input going to it. Always followed by onPause(). |
onPause() | Called when the activity loses foreground state, is no longer focusable or before transition to stopped/hidden or destroyed state. The activity is still visible to user, so it's recommended to keep it visually active and continue updating the UI. Implementations of this method must be very quick because the next activity will not be resumed until this method returns. Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user. |
onStop() | Called when the activity is no longer visible to the user. This may happen either because a new activity is being started on top, an existing one is being brought in front of this one, or this one is being destroyed. This is typically used to stop animations and refreshing the UI, etc. Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away. |
onDestroy() | The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. |
backward compatibility
If you want use fragments in older versions of Android use AndroidX library and class FragmentActivity.
The AppCompatActivity inherits from FragmentActivity and replaces ActionBarActivity. It provides convenience for supporting Material Design style controls.
Add following dependencies in the module-level build.gradle file. It is will be done by default for new projects.
dependencies {
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
// ...
}