在 android(特别是应用程序上下文)中实现构造函数的正确方法是什么?
在android中实现构造函数的正确方法是什么?
似乎在 Activity 或 Service 中“onCreate()”就是神奇发生的地方。
我问的原因是因为我想确定我正在做正确的事情声明 类顶部的属性(特别是 Context),然后在 onCreate 中设置属性值。
// Activity launched via an Intent, with some 'extras'
public class SomeActivity extends Activity {
private Context context;
private String foo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the object attribute for later use, good or Bad to do this?
context = getApplicationContext();
Intent fooIntent = getIntent();
foo = fooIntent.getStringExtra("foo");
}
private void someMethodThatNeedsContext() {
// For example:
Cursor c = this.context.getContentResolver().query(foo, xxx, xxx);
// Or is it better practice to:
// A) Pass the context as a local variable to this method
// B) Use getApplicationContext() locally when needed
}
}
也许这两个选项都可以,但我想太多了? 您可能有的任何具体阅读和/或建议都会对我有很大帮助。
What is the correct way to implement a constructor in android?
It seems that in an Activity or Service 'onCreate()' is where the magic happens.
The reason I ask is because I would like to be sure I'm doing the right thing declaring
attributes in the top of my classes (Context in particular) and then setting the attribute values inside onCreate.
// Activity launched via an Intent, with some 'extras'
public class SomeActivity extends Activity {
private Context context;
private String foo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the object attribute for later use, good or Bad to do this?
context = getApplicationContext();
Intent fooIntent = getIntent();
foo = fooIntent.getStringExtra("foo");
}
private void someMethodThatNeedsContext() {
// For example:
Cursor c = this.context.getContentResolver().query(foo, xxx, xxx);
// Or is it better practice to:
// A) Pass the context as a local variable to this method
// B) Use getApplicationContext() locally when needed
}
}
Maybe either of these options is ok, and I'm over thinking it?
Any specific reading and/or suggestions you may have would greatly be helpful to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您是正确的,初始化应该在
onCreate()
中进行。您实际上既不需要存储对上下文的引用,也不需要调用getApplicationContext()
。您的活动本身就是一个上下文,因此您只需在需要上下文的地方使用即可。例如,在活动中祝酒:Yes, you are correct that initialization is supposed to take place in
onCreate()
. You don't really need neither to store a reference to a context, nor to callgetApplicationContext()
. Your activity is a context itself, so you just use wherever you need a context. For example, making a toast within an activity:选项 B - 因为您可以从 Activity 类中的任何非静态方法调用
getApplicationContext()
。事实上,Activity 也是从 Context 派生的(在继承树中的某个位置..),因此您可以这样做:
您不必保留对上下文的引用。 尤其不是静态的,这可能会导致问题。
你是对的 - 因为你通常不会为 Activity 创建自己的构造函数,所以你将构造代码放在
onCreate
中。Option B - Since you can call
getApplicationContext()
from any non-static methods in your Activity class.In fact, Activity is derived from Context too (Somewhere in the inheritance tree..) so you can just do:
You don't have to keep a reference to a context. Especially not static, that can cause problems.
And you are correct - since you usually don't create your own constructor for Activities, you put the code for construction in
onCreate
.您正在 Activity 中编写一个方法,因此您可以在代码中的任何位置调用
getApplicationContext()
,无需使用局部变量:还要记住 Activity 本身是< /em> 上下文(
Activity
类派生自Context
),因此通常只要需要提供上下文(例如,创建 Intent 时:new意图(这个,...)
)。You are writing a method inside your activity, so you can call
getApplicationContext()
anywhere in your code, you don't need to use a local variable :Also remember that the activity itself is a context (the
Activity
class is derived fromContext
), so generally you can usethis
whenever you need to provide a context ( for example when creating an Intent :new Intent(this, ...)
).