Like the lifecycle of UI controllers in android studio, I want to know if there is any lifecycle-like concept for java classes in android studio?
Taking the example of onCreate
method of the activity below:-
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
}
So the question is, if Java classes also have any states like this?
Java classes don't really have a lifecycle. They're constructed when you call the constructor, they last until the garbage collector says they can be deleted, and then the memory is freed. There is a finalizer that's called right before that happens, but using it is discouraged without a real need- it slows the garbage collector down significantly.
The onCreate of Activity works just like a normal function does. Its being called by the class that created the instance of the Activity. This class is part of the Android framework, but is just another normal Java class. It basically goes:
MyActivity activity = new MyActivity();
activity.onCreate(bundle);
//Does some other stuff
oldActivity.onPause()
activity.onStart()
activity.onResume()
oldActivity.onStop()
You can apply the same discipline to objects if you have a need, but its not built in for anything. Not even Activity- its just the framework class that launches activities handling this kind of behavior.