在 android(特别是应用程序上下文)中实现构造函数的正确方法是什么?

发布于 2024-12-21 22:16:54 字数 1027 浏览 0 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

踏雪无痕 2024-12-28 22:16:54

是的,您是正确的,初始化应该在 onCreate() 中进行。您实际上既不需要存储对上下文的引用,也不需要调用 getApplicationContext()。您的活动本身就是一个上下文,因此您只需在需要上下文的地方使用即可。例如,在活动中祝酒:

Toast.makeToast(this, "Some text", Toast.LENGTH_LONG).show();

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 call getApplicationContext(). Your activity is a context itself, so you just use wherever you need a context. For example, making a toast within an activity:

Toast.makeToast(this, "Some text", Toast.LENGTH_LONG).show();
遥远的绿洲 2024-12-28 22:16:54

选项 B - 因为您可以从 Activity 类中的任何非静态方法调用 getApplicationContext()
事实上,Activity 也是从 Context 派生的(在继承树中的某个位置..),因此您可以这样做:

Cursor c = getContentResolver()....

您不必保留对上下文的引用。 尤其不是静态的,这可能会导致问题。

你是对的 - 因为你通常不会为 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:

Cursor c = getContentResolver()....

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.

探春 2024-12-28 22:16:54

您正在 Activity 中编写一个方法,因此您可以在代码中的任何位置调用 getApplicationContext(),无需使用局部变量:

Cursor c = getApplicationContext().getContentResolver().query(foo, xxx, xxx);

还要记住 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 :

Cursor c = getApplicationContext().getContentResolver().query(foo, xxx, xxx);

Also remember that the activity itself is a context (the Activity class is derived from Context), so generally you can use this whenever you need to provide a context ( for example when creating an Intent : new Intent(this, ...)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文