where is best place to get the resources in the activity

52 views Asked by At

i want to know where is the best place i should get the resources of the views in android..

for example should i get the resource in onCreate() or onStart();

3

There are 3 answers

0
Dheerendra Mitm On BEST ANSWER

When an activity transitions into and out of the different states described above, it is notified through various callback methods. All of the callback methods are hooks that you can override to do appropriate work when the state of your activity changes. The following skeleton activity includes each of the fundamental lifecycle methods:

public class ExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // The activity is being created.
}
@Override
protected void onStart() {
    super.onStart();
    // The activity is about to become visible.
}
@Override
protected void onResume() {
    super.onResume();
    // The activity has become visible (it is now "resumed").
}
@Override
protected void onPause() {
    super.onPause();
    // Another activity is taking focus (this activity is about to be "paused").
}
@Override
protected void onStop() {
    super.onStop();
    // The activity is no longer visible (it is now "stopped")
}
@Override
protected void onDestroy() {
    super.onDestroy();
    // The activity is about to be destroyed.
}

}

onCreate() : Called when the activity is first created. This is where you should do all of your normal static set up — create views, bind data to lists, and so on. This method is passed a Bundle object containing the activity's previous state, if that state was captured (see Saving Activity State, later). Always followed by onStart().

onStart() : Called just before the activity becomes visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

http://developer.android.com/guide/components/activities.html

0
Gabriella Angelova On

I think that the best place is in the onCreate() method (right after the set of the context), because there is the place to load everything and this is the method that is called when you start an activity no matter how.

Here you could see the schema from the original documentation where it is shown that the onCreate method is before the onStart method, so I think this should be the place for initialization http://developer.android.com/reference/android/app/Activity.html

And some text from the documentation:

onCreate(): Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.

0
shrishail_Android1490711 On

onCreate(); ofcourse. This is where your activity is created and before it is about to visible literally on sccreen of phone you have to declare it.