how to call a non static method from Activity?
I have a variable to set in one of my activities -Say myActivity. I have a met开发者_高级运维hod that set this variable. Before loading the activity I want to set the variable. Do I have any chance to reference somewhere to my activity, or do I have to use intents?
You can also use some variables in the application object.
Before loading the activity I want to set the variable.
Do you mean, before you start you activity (i mean a call of startActivity())?
Whenever you need to initialize some variables or other resources in an activity, you should always do that inside the onCreate()
method. This method is provided specifically for this initialization purpose. Since this method is not static, you won't have any problems to call other non-static methods from within.
Now if some variable's initial value comes from outside that activity, you need to use intents to pass that data to that activity, catch it inside the onCreate
method, and then initialize the variable with it.
Take a look at this Activity Life-cycle Diagram to better understand the life-cycle of an activity.
精彩评论