Description for Add New Activity and Activity Life Cycle in Android using Kotlin:

ADD NEW ACTIVITY AND ACTIVITY LIFE CYCLE:–
Part 1
1 create new activity in Android in different ways
2 understand the cycle of an activity
Part 2
3 navigation between activities back and fourth
4 passing data between activity
Part 3
5 formatting activity
a apply custom toolbar
b change the title of activity
c create menu item add menu items in activity and work on menu item event
d enable back navigation button on second activity
e get data from activity to activity
f apply theme on activity
Part 4
6 permission handling in Activity

Steps to add new activity
First step is to add new activity
Project ->app-> Java ->com. package-> new->empty activity
And give name it to second activity

Add this activity to the manifest file
create new layout for third activity
Layout -> new -> layout resource file -> give name it to activity third

In activity main xml- 
Add text view in all three activities
For first  one I am first activity
For second I am second activity
For third i am third activity
Add third activity 
Package.com-> new->kotlin class -> name give it as third activity
Write code given below
In Android manifest xml
Write code given below
<activity andriod : name ” third activity>
</activity> same as second activity
In main activity
 
 life cycle
As a user navigates through the app, Activity instances in your app transition through different stages in their life-cycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides. Write code given below
class main activity: appcompatactivity()
Val TAG =”main activity”
override fun on create(saved instance state :bundle?){
super on create(create saved instance state}
set content view(R.layout.activity_main)
Log.d(TAG msg ” I am on create”}
textview _first.setonclicklistener
{
Val intent=intent(package content this second activity::class.java;
Start activity content
}
Override fun on start(){
Super.on start ()
Log.d(TAG,msg ” I am on start”)
}
Override fun on Restart(){
Super. On Restart()
Log.d(TAG msg “I am in Restart”)
}
Override fun on resume(){
Super.onResume()
Log.d(TAG msg.”I am is resume”}
}
Override fun on pause(){
super.on pause()
Log.d(TAG msg “I am in onpause”)
}
}
Override fun on stop(){
Super.onstop
Log.d(TAG msg “I am in on stop”
}
Override fun on destroy(){
Super.ondestroy ()
Log.d(TAG msg”I am in destroy”)
}
}

Copy this code in mainkt file for activity second and third
Replace the textviewid with ids of these activities
Replace Val TAG also
In activity main xml- give I’d to each textview for all activities

https://youtu.be/C3YBOZ6tKEc

Leave a Reply

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