Activity and its
Lifecycle
What is an Activity?
Which
present something to you with which you can interact there call Activity.An
application component that provides a screen
What they do.
You can draw
different type of UI on its windows.Every app has 1 main activity and other
activities.An ap can start any Activity belonging to certain conditions.When a
new Activity starts,the previous Activity is stopped and added to stack knows
as BackStack.
What is a callback method?
Android OS
calls certain method on your Activity class to notify whether your app is
running currently or not.
Just like
JVM calling public static void main
General guidelines
1.
Don’t do heavy processing or network consuming
operations when user is currently away from your app.
2.
App should not crash when another app is
started.
3.
Don’t lose the user’s progress or session data.
Activity Lifecycle Methods
When user
launch your apps for the first time now the user can launch your app from the
app section or he can launch through widget or whatever so what happen when the
user click your app icon. These three methods are called onCreate (), onStart
(), onResume ().What happens when the user actually try to pause your app. So
in this case onPause (), onStop () method are called. What happens when the
user click the back button. So in this case these methods are called onRestart
(), onStart (), onResume () in a quick.
What is Logcat?
Logcat is
used for debugging purposes. We will use the logcat in activity to check the
Activity Lifecycle Method. Logcat Print different messages using
android.util.Log class.
Log.d
(String tag, String message)
Log.d
(“LIFECYCLE”, ”onCreate was called”);
There are
other method in Logcat
1. For
information we can use Log.i(String tag, String message)
2. For Error
we can use Log.e(String tag, String message)
3. For warning
we can use Log.w(String tag, String message)
4. For Verbose
we can use Log.v(String tag, String message)
Activity Lifecycle Diagram
Example:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Android is simples","onCreate was called");
}
@Override
protected
void onResume() {
super.onResume();
Log.d("Android is simples","onResume was called");
}
@Override
protected
void onStart() {
super.onStart();
Log.d("Android is simples","onStart was called");
}
@Override
protected
void onPause() {
super.onPause();
Log.d("Android is simples","onPause was called");
}
@Override
protected
void onStop() {
super.onStop();
Log.d("Android is simples","onStop was called");
}
@Override
protected
void onRestart() {
super.onRestart();
Log.d("Android is simples","onRestart was called");
}
@Override
protected
void onDestroy() {
super.onDestroy();
Log.d("Android is simples","onDestroy was called");
}
}
No comments:
Post a Comment