Create an android app that demonstrates Activity Lifecycle and Instance State.
In this tutorial we will see the working of Activity lifecycle step by step. Activity life cycle consists of 7 methods as below.
- onCreate() - Invoked when Activity is created.
- onStart() - Invoked when Activity is becoming visible.
- onResume() - Invoked when Activity starts Interacting with user.
- onPause() - Invoked when Activity is invisible to user
- onStop() - Invoked when activity is no longer visible.
- onRestart() - Invoked after your activity is stopped, prior to start.
- onDestroy() - Invoked before Activity is killed.
Let's begin coding....π»π»
Step 1: After creating Android project, open activity_main.xml file and paste below code as it is.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.javatpoint.com.activitylifecycle.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Step 2: Now open MainActivity.java file and paste below code.
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast.makeText(MainActivity.this,"onCreate invoked",Toast.LENGTH_SHORT).show(); Log.d("lifecycle","onCreate invoked"); } @Override
protected void onStart() { super.onStart(); Toast.makeText(MainActivity.this,"onStart invoked",Toast.LENGTH_SHORT).show(); Log.d("lifecycle","onStart invoked"); } @Override protected void onResume()
{ super.onResume(); Toast.makeText(MainActivity.this,"onResume invoked",Toast.LENGTH_SHORT).show(); Log.d("lifecycle","onResume invoked"); } @Override protected void onPause() { super.onPause(); Toast.makeText(MainActivity.this,"onPause invoked",Toast.LENGTH_SHORT).show(); Log.d("lifecycle","onPause invoked"); } @Override protected void onStop() { super.onStop(); Toast.makeText(MainActivity.this,"onStop invoked",Toast.LENGTH_SHORT).show(); Log.d("lifecycle","onStop invoked"); } @Override protected void onRestart() { super.onRestart(); Toast.makeText(MainActivity.this,"onRestart invoked",Toast.LENGTH_SHORT).show(); Log.d("lifecycle","onRestart invoked"); } @Override protected void onDestroy() { super.onDestroy(); Toast.makeText(MainActivity.this,"onDestroy invoked",Toast.LENGTH_SHORT).show(); Log.d("lifecycle","onDestroy invoked"); } }
Step 3: wohoo!! we are done with the coding in just two steps. Now run the project and see output in Logcat.
Note: Logcat is located at the bottom of the Android Studio.
Enjoy coding!!!π
great
ReplyDeletethnx
DeleteNicee
ReplyDeleteNice...
ReplyDeleteShort and simple.
ReplyDeleteShort and simple... Easy to understand..
ReplyDeleteThnq all
ReplyDeleteToo much easy to understand and completely sorted steps, awesome one ma'am, I would like to have more blogs such like this
ReplyDeleteGreat π
ReplyDeleteGreat π
ReplyDeleteMast madam...good going
ReplyDeleteThnq sir
DeleteEasy to understand
ReplyDeleteEasy to understand...
ReplyDeleteYeah, easy to understand. But i think database is more suitable for me π π
ReplyDeletethq vedant. I will post Databse related blogs too.
DeleteYes, thanks, I'm working on mysql, mongodb, PostgreSQL Databases.
DeleteNice. Great job Bhakti.
ReplyDeleteExplained really well step by step. Keep posting such amazing tutorials ��
ReplyDeleteYea sure
DeleteGood job Bhakti.Keep it up. Short and simple steps. Very easy to understand.
ReplyDeletethnq
DeleteHelpful content
ReplyDeletethnq
DeleteGreatπ
ReplyDeleteVery helpful content for us so thank alot...π
ReplyDeleteGood One
ReplyDeleteWaw very nice .. keep it up sister π
ReplyDeleteExplained very well in short and simple steps. Helpful content ma'am π
ReplyDeletethank you all
ReplyDeleteVery Helpful Content ππ Keep it up Sisterππ
ReplyDeleteVery helpful content to understand the process along with it's proper working code!!!Thank youπ
ReplyDeleteThankyou mam... Quite clear with these concepts... π
ReplyDeleteVery helpful content
ReplyDeleteThank you mam