oncreate
Using onCreate vs. onRestoreInstanceState
onRestoreInstanceState This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide … Read more
what is the different between onCreate() and onCreateView() lifecycle methods in Fragment?
onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here. It is always called some time after the onCreate … Read more
After the rotate, onCreate() Fragment is called before onCreate() FragmentActivity
You should not count on a valid Activity until the onActivityCreated() call in the Fragment’s life cycle. Called when the fragment’s activity has been created and this fragment’s view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. The exact reasons … Read more
Null pointer Exception – findViewById()
findViewById() returns a View if it exists in the layout you provided in setContentView(), otherwise it returns null and that’s what happening to you. Note that if you don’t setContentView(), and don’t have a valid view to findViewById() on, findViewById() will always return null until you call setContentView(). This also means variables in the top-level … Read more
When does Application’s onCreate() method get called?
Only the first time. When Activity is started and application is not loaded, then both onCreate() methods will be called. But for subsequent starts of Activity, the onCreate() of application will not be called.
onCreate flow continues after finish()
I’m guessing that it is because finish() doesn’t cause the onCreate method to return. You could try simply adding finish(); return; Or use an if else @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layoutb); if(good data){ //do stuff }else{ finish(); } }
Android – Activity Constructor vs onCreate
I can’t think of any good reason to do anything in the constructor. You never construct an activity directly, so you can’t use it to pass in parameters. Generally, just do things in onCreate.
super.onCreate(savedInstanceState);
Every Activity you make is started through a sequence of method calls. onCreate() is the first of these calls. Each and every one of your Activities extends android.app.Activity either directly or by subclassing another subclass of Activity. In Java, when you inherit from a class, you can override its methods to run your own code … Read more