Issue
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();
}
}
Solution
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.
Answered By - Onik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.