Different States of an Android Activity
Methords
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState");
}
If you have abstract methods to implement, press Alt+Enter over class name to open a wizard. Otherwise, for overriding a function, just start typing the name of your method (without any access attribute or type), anywhere in your class scope. Then, Android Studio should suggest you overridable methods.
-------------------------------------------------------------------------------------------------------------------------
Method Overriding in Java
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.
In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.
Usage of Java Method Overriding
- Method overriding is used to provide specific implementation of a method that is already provided by its super class.
- Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
- method must have same name as in the parent class
- method must have same parameter as in the parent class.
- must be IS-A relationship (inheritance).
-------------------------------------------------------------------------------------------------------------------------
Example of adding different activity in my android application
package com.example.root.myfirstandroidapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.util.Log; public class MainActivity extends AppCompatActivity { private static final String TAG = "MyActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i(TAG,"onCreate"); } @Override protected void onStart() { super.onStart(); Log.i(TAG, "onStart"); } @Override protected void onResume() { super.onResume(); Log.i(TAG, "onResume"); } @Override protected void onPause() { super.onPause(); Log.i(TAG, "onPause"); } @Override protected void onStop() { super.onStop(); Log.i(TAG, "onStop"); } @Override protected void onRestart() { super.onRestart(); Log.i(TAG, "onRestart"); } @Override protected void onDestroy() { super.onDestroy(); Log.i(TAG, "onDestroy"); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.i(TAG, "onSaveInstanceState"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i(TAG, "onRestoreInstanceState"); } public void onButtonClick(View V){ EditText e1 = (EditText)findViewById(R.id.editText); EditText e2 = (EditText)findViewById(R.id.editText2); EditText e3 = (EditText)findViewById(R.id.editText3); int num1 = Integer.parseInt(e1.getText().toString()); int num2 = Integer.parseInt(e2.getText().toString()); int sum = num1+num2; e3.setText(Integer.toString(sum)); } }
Log details of above code run
06-29 11:12:48.872 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onCreate
06-29 11:12:48.872 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onStart
06-29 11:12:48.872 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onResume
06-29 11:28:18.959 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onPause
06-29 11:28:19.037 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onStop
06-29 11:28:19.037 2996-2996/com.example.root.myfirstandroidapplication I/MyActivity: onDestroy