Android 中的辅助类有什么约定吗?

发布于 2024-12-05 16:16:28 字数 1079 浏览 1 评论 0原文

对于添加到应用程序中的每个活动,我注意到在活动的初始化中使用了很多类似的代码。具有静态方法的帮助器类来包装类似的代码似乎是可行的方法。

我首先想到的是单例类。我可以添加静态方法/变量并在整个应用程序中使用它们。我还没有真正尝试过看看这在 Android 应用程序中是如何工作的。进一步搜索后,我看到了一些有关创建扩展 Application 的类的信息。为此我做了一个简单的测试:

public class MyApp extends Application {
    public static String DEMOTEXT = "WORKING!";

    public static void ShowToast(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }
}

MyApp.ShowToast(this, MyApp.DEMOTEXT); // Placed on onCreate of some Activity

这完全符合我的预期。这是 Android 上的做法还是有更好的约定?这样做时我还应该考虑什么?

顺便问一下,我应该在字符串上使用 final 关键字吗?方法又如何呢?

编辑:我刚刚读过这个:

通常不需要子类化应用程序。在大多数情况下, 静态单例可以以更加模块化的方式提供相同的功能 方式。如果您的单例需要全局上下文(例如注册 广播接收器),检索它的函数可以给出 内部使用 Context.getApplicationContext() 的上下文 首先构建单例。

http://developer.android.com/reference/android/app/Application.html

那我应该使用单例吗?

For every Activity I add to my app I'm noticing a lot of similar code being used in the initialization of the Activity. A helper class with a static method to wrap this similar code seems the way to go.

I first thought of a singleton class. I could add static methods/variables and use them across the application. I haven't really tried to see how would this work in an Android application. Searching a little bit more I saw something about creating a class extending Application. For this I did a simple test:

public class MyApp extends Application {
    public static String DEMOTEXT = "WORKING!";

    public static void ShowToast(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }
}

MyApp.ShowToast(this, MyApp.DEMOTEXT); // Placed on onCreate of some Activity

This works exactly as I expected. Is this the way to go on Android or is there a better convention? Anything else I should consider when doing this?

By the way, should I use the final keyword on the string? What about the method?

EDIT: I just read this:

There is normally no need to subclass Application. In most situation,
static singletons can provide the same functionality in a more modular
way. If your singleton needs a global context (for example to register
broadcast receivers), the function to retrieve it can be given a
Context which internally uses Context.getApplicationContext() when
first constructing the singleton.

http://developer.android.com/reference/android/app/Application.html

Should I use a singleton then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

扮仙女 2024-12-12 16:16:28

Application 主要用于全局应用程序初始化。您将创建自己的类,覆盖 Application.onCreate() 并在那里初始化您的静态应用程序数据。

不要忘记在 AndroidMainfest.xml 中声明它:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:name="your.package.path.to.MyApp">

静态帮助器类按照您的方式创建。
约定是在第一个位置使用小写字母,因此 MyApp.showToast(...)

如果您想避免在其他地方进行修改(因为它应该是一个常量),您可以对字符串使用final

// this would allow ...
public static String DEMOTEXT = "WORKING!";

// ... to do this somewhere else
MyApp.DEMOTEXT = "NOT WORKING!"

Application is primarily used for a global application initialization. You would create your own class, override Application.onCreate() and initialize your static application data there.

Dont forget to declare it in the AndroidMainfest.xml:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:name="your.package.path.to.MyApp">

A static helper class is made the way you did.
The convention is to use lower case letter at first position, so MyApp.showToast(...).

You would use final for the String if you would want to avoid madifications on other places (since it should be a contant).

// this would allow ...
public static String DEMOTEXT = "WORKING!";

// ... to do this somewhere else
MyApp.DEMOTEXT = "NOT WORKING!"
宛菡 2024-12-12 16:16:28

我还没有尝试过,但我认为你也应该能够做这样的事情。

public class MyActivity extends Activity {
    private static final String DEMOTEXT = "WORKING!";

    @Override
    public void onCreate(Bundle bundle) 
    {
        super.onCreate(bundle);
        Toast.makeText(this, DEMOTEXT, Toast.LENGTH_SHORT).show();
    }
}

现在,对于需要使用该初始化的所有活动,只需扩展您的基本活动类即可。

public class SomeActivity extends MyActivity {
    ...
    // Should display the toast on create
    ...
}

I haven't tried this but I think you should be able to do something like this as well.

public class MyActivity extends Activity {
    private static final String DEMOTEXT = "WORKING!";

    @Override
    public void onCreate(Bundle bundle) 
    {
        super.onCreate(bundle);
        Toast.makeText(this, DEMOTEXT, Toast.LENGTH_SHORT).show();
    }
}

Now for all activities that need to use that initialization could just extend your base activity class.

public class SomeActivity extends MyActivity {
    ...
    // Should display the toast on create
    ...
}
祁梦 2024-12-12 16:16:28

是的,只需使用单例。在这种情况下,如果您的方法是静态的,您甚至不需要单例。只是一个具有静态方法的类。

Yes just use a singleton. Well in this case if your methods are static, you don't even need a singleton. Just a class with static methods.

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