1
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    }
}

When I see this basic source code in MyActivity.java, onCreate() method is overriding just. but When I run the app, I can see that overrided method "onCreate()" runs. how is this possible?

If its possible to run the onCreate method in that code, I thought there should be a code like onCreate();

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Noder
  • 313
  • 3
  • 15
  • 1
    The way it works is due to the lifecycle of an Activity component, magic is done by `super.onCreate(savedInstanceState)` – Aseem Sharma Oct 06 '18 at 13:22
  • @AseemSharma I didn't understand it yet. because I think super.onCreate(savedInstanceState) came from super class which is AppCompatActivity – Noder Oct 06 '18 at 13:24
  • 1
    super.onCreate(savedInstanceState) is doer. if you understand OOP then you ought to be get it. – Zaid Mirza Oct 06 '18 at 13:29
  • 1
    It's triggered from the moment you register MainActivity in Manifest with Intent Filters action_MAIN and category_LAUNCHER, it will ask the Main Thread to launch your MainActivity and by launching it mean going through the lifecycles of the activity which is started by `onCreate` method. – Aseem Sharma Oct 06 '18 at 13:34
  • 1
    Here is a good article to understand how things are working from the start. [link](https://medium.com/@martinomburajr/android-internals-1-how-android-starts-your-main-activity-8fcf80e65222) – Aseem Sharma Oct 06 '18 at 13:37

2 Answers2

2

We can always override these functions and add more to it but the Question is how are these functions called automatically when no one is calling them? We haven’t written any code to call them.

This is where the concept of CALLBACK FUNCTIONS comes in.

The concept of callbacks is to inform a class synchronous / asynchronous if some work in another class is done. Some call it the Hollywood principle: "Don't call us we call you".

Here's a example:

class A implements ICallback {
     MyObject o;
     B b = new B(this, someParameter);

     @Override
     public void callback(MyObject o){
           this.o = o;
     }
}

class B {
     ICallback ic;
     B(ICallback ic, someParameter){
         this.ic = ic;
     }

    new Thread(new Runnable(){
         public void run(){
             // some calculation
             ic.callback(myObject)
         }
    }).start(); 
}

interface ICallback{
    public void callback(MyObject o);
}

Class A calls Class B to get some work done in a Thread. If the Thread finished the work, it will inform Class A over the callback and provide the results. So there is no need for polling or something. You will get the results as soon as they are available.

In Android Callbacks are used f.e. between Activities and Fragments. Because Fragments should be modular you can define a callback in the Fragment to call methods in the Activity. copied from here

for more study follow this link please :

link 1

link 2

Tanim reja
  • 2,120
  • 1
  • 16
  • 23
1

The onCreate method is called during the Activity Lifecycle. The docs regarding this method state

You must implement this callback, which fires when the system first creates the activity

So the point of this method is for you to initialize anything specific to your activity that needs to be done when it is first created, and call super to propagate this to it's superclasses, allowing them to perform their initialization sequence as well. You should not be invoking this method yourself.

Danny Buonocore
  • 3,731
  • 3
  • 24
  • 46