The below is the Android Life Cycle, it explains how the execution flow in the android. Every Programing language has a life cycle.
package biz.standardservices.lifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("In onStart()");
Log.d("Starting", "onstart() called");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d("Resuming","onResume() called");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d("Pausing","onPause() called");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d("Stoppping","onStop() called");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d("Restarting","onRestart() called");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("Destroying", "onDestroy() called");
}
}
Output in Log Cat:
What is LogCat:
Logcat is a console to identify the execution flow and to find the errors where exactly occured.
It is used for debugging.
Print different messages to Log cat using android.util.Log class
Syntax :
Log.d(String tag,string message);
ex:
Log.d("Destroying", "onDestroy() called");
Note :
What is Life Cycle?
Life Cycle which is explains the born to death process (in technically program started some activities done and closed) about one thing.Android Life Cycle:
The below is the flow of life cycle- onCreate()
- onStart()
- onResume()
App is Running state - onPause()
- onStop
- onRestart
- onDestroy()
package biz.standardservices.lifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("In onStart()");
Log.d("Starting", "onstart() called");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d("Resuming","onResume() called");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d("Pausing","onPause() called");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d("Stoppping","onStop() called");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d("Restarting","onRestart() called");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("Destroying", "onDestroy() called");
}
}
Output in Log Cat:
Logcat is a console to identify the execution flow and to find the errors where exactly occured.
It is used for debugging.
Print different messages to Log cat using android.util.Log class
Syntax :
Log.d(String tag,string message);
ex:
Log.d("Destroying", "onDestroy() called");
Note :
Log.v(); // Verbose
Log.d(); // Debug
Log.i(); // Info
Log.w(); // Warning
Log.e(); // Error
In Detail
- Log.e: This is for when bad stuff happens. Use this tag in places like inside a catch statment. You know that an error has occurred and therefore you're logging an error.
- Log.w: Use this when you suspect something shady is going on. You may not be completely in full on error mode, but maybe you recovered from some unexpected behavior. Basically, use this to log stuff you didn't expect to happen but isn't necessarily an error. Kind of like a "hey, this happened, and it's weird, we should look into it."
- Log.i: Use this to post useful information to the log. For example: that you have successfully connected to a server. Basically use it to report successes.
- Log.d: Use this for debugging purposes. If you want to print out a bunch of messages so you can log the exact flow of your program, use this. If you want to keep a log of variable values, use this.
- Log.v: Use this when you want to go absolutely nuts with your logging. If for some reason you've decided to log every little thing in a particular part of your app, use the Log.v tag.
- Log.wtf: Use this when stuff goes absolutely, horribly, holy-crap wrong. You know those catch blocks where you're catching errors that you never should get...yea, if you wanna log them use Log.wtf
0 comments:
Post a Comment