Java Variable Declaration

I got a little confused in the android activity program thread, in java the main method is executed first, and onCreate methods are run in android first, so what about the variables that are defined at the class level? How they are declared / initialized when an action begins to execute, stating that onCreate is called first. Also Can we initialize these variables in the Inner class and use their values ​​outside the inner class?

+4
source share
3 answers

In fact OnCreate(), this is the method that is called first, as you say. Activity moves like this

OnCreateOnStartOnResume

At this point, the activity is visible to the user

, Activity

OnPauseOnStopOnDestroy

, . . - , .. OnStart OnDestroy. OnCreate, , . , OnResume. Activity

Lifecycle

, java

+5

onCreate() Android. - Android Context, , .

. . , .

0

If you want to access a variable from anywhere in the class, then you must declare this class name declaration of the variable, and you can initialize this variable from anywhere in your class relative to your variable value ... below, there is a sample plan ...

class MainActivity extends Activity {

    //Declare your variable here

    @Override
    protected void onCreateActivity(Bundle savedInstanceState) {

        //Initialize your variable here

   }

} 
0
source

Source: https://habr.com/ru/post/1525528/


All Articles