Reputation: 305
I'm taking an Android Studio class, and the teacher taught me to do
Log.i("data", "onCreate called")
in the overriden methods. I just don't understand why.
This is the code of the whole thing:
package com.myfirstapplication.theclass4
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.i("data", "onCreate called") //Log turns red -> Alt+Enter
}
override fun onStart() {
super.onStart()
Log.i("data", "onStart called")
}
override fun onResume() {
super.onResume()
Log.i("data", "onResume called")
}
override fun onPause() {
super.onPause()
Log.i("data", "onPause called")
}
override fun onStop() {
super.onStop()
Log.i("data", "onStop called")
}
override fun onDestroy() {
super.onDestroy()
Log.i("data", "onDestroy called")
}
}
Upvotes: 1
Views: 3379
Reputation: 3712
You'll be needing something called "logcat" a lot whenever you're developing for Android.
Whenever you open Android Studio, you'll see something like this:
These are different levels of "problem"/information that you're logging, so that you can understand which code is being run, and if it is working as expected.
Now, you said why does Log.i("key","value")
exist, and why you were told to do that.
It simply means, you logged an event, which was just for purposes of logging normal "information", as "i" in Log.i
stands for Info.
There are different levels of severity of an event that you're trying to log.
So, for example, if you want to log an event that causes your app to crash, you'll log it with Log.e("key","value");
, where "e" signifies and error.
There are different levels of severity which you can find here.
Now, why do you need these different levels?
Simply, because you need to filter those depending on what you're trying to log. So if for example your application crashes, you'll be able to see it in "Error" section in the dropdown that is marked in the picture above. So you can say it's for organizing the data that you/the system is logging.
The first parameter in Logs is the key, so you can search for particular logs belonging to one category quickly through the search bar in logcat, the second parameter is for adding more information.
Example:
void multiply(){
int a = 3*4;
Log.i("multiplication","Value of a is:"+String.valueOf(a));
}
In your example, you were told to log stuff because your teacher was trying to teach you lifecycle of an activity, meaning, in which order the functions of activity are executed.
Upvotes: 0
Reputation: 21753
It means log a message to the LogCat with the info severity.
This information can be easily found in the docs but you can read more about logcat in this doc
Upvotes: 0