Activities in Android

 What is an Activity in Android?

  • An Activity is a single screen with a user interface in an Android app.
  • It is like a window in a desktop app, but full-screen on mobile.
  • Activities let users interact with your app.
  • Each Activity handles its own UI and logic.

Key Features of Activity

  • Represents one screen of the app.
  • Manages UI components (like buttons, text fields).
  • Handles user interaction.
  • Works with intents to navigate to other activities.
  • Has a lifecycle (created, started, resumed, paused, stopped, destroyed).


Activity Lifecycle

Android manages Activity states:

  • onCreate():Called when Activity is created. Initialize UI.
  • onStart():Activity becomes visible.
  • onResume():Activity is in foreground, ready for user.
  • onPause():Another Activity comes in front.
  • onStop():Activity no longer visible.
  • onDestroy():Activity is removed from memory.

Intent

Activities use Intents to start other Activities.

binding.btnActivity.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}







Activity Initializing with Binding

at build.gradle.kts(Module:app)
android {
//
buildFeatures {
viewBinding = true
}

}

Activity with binding:-
package com.app.shortnewstestuser.ui.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.app.shortnewstestuser.databinding.ActivityMainBinding


class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)


//

}

}








No comments:

Post a Comment