-1

I am new to android and I am learning android by creating various basic applications to clear my understanding. I have gone through android life cycle so I tried to write below codes.

First one is normal with onCreate() method and Second one is just created with onStart() method but without onCreate() method but still both works properly without any error.

My doubt: How application can start without onCreate() method with Activity as Superclass. Is it not wrong according to life cycle? (But still it is working !)

First One:

import android.content.Intent;
import android.net.Uri;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:+91**********"));
        startActivity(intent);
        finish();
    }
}

Second One:

import android.content.Intent;
import android.net.Uri;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:+91**********"));
        startActivity(intent);
        finish();

    }
}
Onik
  • 19,396
  • 14
  • 68
  • 91
  • possible duplicate of [Android activity life cycle - what are all these methods for?](http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for) Take a look at this question, there are great explanations about each one and why you should start your views on onCreate and the activity lifecycle – George Aug 23 '15 at 20:32

3 Answers3

2

How application can start without onCreate()...

Without being overridden, the super Activity's onCreate() will be called instead.

Is it not wrong according to life cycle?

From the point of view of OOP, you did nothing wrong. But, from the point of view of Activity's lifecycle, not overriding the onCreate() method is not what "you're expected to do". You should stick to the lifecycle methods as per the official documentation or numerous posts on the Internet in order to manage Activity's states properly.

For instance, in your case you're making a call from improper Activity's state, unless you intended to do so. According to the Activity's lifecycle the onCreate() method is called just once, - on Activity instance creation, while the onStart() method may be called multiple times during its lifecycle.

Onik
  • 19,396
  • 14
  • 68
  • 91
0

If you remove OnCreate it doesn't mean that this method is not invoked - you just don't have it overrided.

Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84
0

The difference is that the onStart() will be triggered everytime the app triggers the onRestart(), e.g when you take the activity out of visibility than take it back to visibility.

The onCreate() happens only once, when the activity is created, or when you kill the app then recreate it.

And the onStart() happens after the onCreate(), as you can see in this lifeCycle

Geraldo Neto
  • 3,670
  • 1
  • 30
  • 33