In FragmentActivity, the order of super.onCreate and setContentView isn't important, why?
FragmentActivity
//OK
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_waiting_for_confirmation_order);
}
//OK
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_waiting_for_confirmation_order);
super.onCreate(savedInstanceState);
}
But in ActionBarActivity, it throws a NullPointerException.
ActionBarActivity
//OK
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_waiting_for_confirmation_order);
}
//ERROR
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_waiting_for_confirmation_order); //NullPointerException
super.onCreate(savedInstanceState);
}
The reason is ActionBarActivity (from support-v7) is using a delegate object to either use the real implementation or the compat implementation.
This delegate is instantiated in the method onCreate() of ActionBarActivity and the method setContentView() of ActionBarActivity is simply doing delegate.setContentView().
That's why there's a NPE if you call setContentView() before onCreate().
In FragmentActivity, (or standard Activity actually), the order doesn't matter because setContentView() doesn't rely on a specific object that could have been instantiated in onCreate().
Related
I extend all my activities from BaseClass. Thus i want to initialize common variables in onCreate() of BaseActivity class. However, super.onCreate(savedInstanceState); does not call immediately the parent class's onCreate() method. Why?
public class SplashActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // after this line executed, next line executed rather than the onCreate() of parent BaseActivity
...
}
}
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
...
}
}
I have the following code:
public class MainActivity extends Activity {
TextView number=(TextView)findViewById(R.id.number2);
TextView number2=(TextView)findViewById(R.id.number2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number.setText("Text");
number.setText("Text");
}
followed by more code, but when I run it it crashes.
After doing that i tried to initialize TextViews in onCreate()
public class MainActivity extends Activity {
TextView number;
TextView number2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number=(TextView)findViewById(R.id.number);
number2=(TextView)findViewById(R.id.number2);
number.setText("Text");
number.setText("Text");
}
and it worked. Why must objects be initialized in onCreate?
Your activity won't have a Window until onCreate(). Attempting to find any views before the window is initialized will lead to NPE.
Additionally, attempting to find views before setContentView() will return nulls and so the return values are not good for anything.
public class MyClassActivity<T> extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// todo..
}
It is possible?
How startActivity with T object?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.savedInstanceState = savedInstance;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
try {
Log.d("Equals?",savedInstanceState.equals(this.savedInstanceState));
} catch (Exception e) {
Log.d("Equals?",savedInstanceState==this.savedInstanceState);
}
}
Will this always\never or only on some cases log true?
EDIT:
after seeing Henry's comment, lets address my question as if Intent has overriden its equals and it does compare those objects content, not by references...
onCreate(Bundle) called to do initial creation of the fragment.
onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().
so we can say that Bundle savedInstanceState are same at both places.
for more visit
http://developer.android.com/reference/android/app/Fragment.html
http://developer.android.com/guide/components/fragments.html
you wrote
#Override
public void onCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.savedInstanceState = savedInstance;
}
i do not understand which lifecycle mehod is onCreated i think it should be onCreate and the lines should be
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....}
Is it possible to set the titlebar in every activity programmatiaclly from one particular activity?
I can set the titlebar of a particular activity programmatically from within that activity, but subsequent activities have the string set from the manifest thereafter.
To set a title I use the following from within the activity. I would like every activity set like this in the application.
setTitle(carerName + " is now logged in");
you can write base activity and call a method of super .
public class DerivedActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...}
}
public class DerivedActivity2 extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...}
}
public class BaseActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(carerName + " is now logged in");
}
}