提到过活动的工具类的android [英] Passing reference to activity to utility class android

查看:196
本文介绍了提到过活动的工具类的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问了很多次,但我仍然无法完全理解这个概念。在我的应用程序,我使用的是静态的工具类,以保持通用的方法(如显示错误对话框)

I realise this question has been asked many times, but I am still unable to completely understand this concept. In my application, I am using a static utility class to keep common methods (like showing error dialogs)

下面是我的静态类是这样的:

Here is how my static class looks like:

    public class GlobalMethods {

//To show error messages
        public static final void showSimpleAlertDialog(final Activity activity, String  message, final boolean shouldFinishActivity) {

            if (!activity.isFinishing()) {

                AlertDialog.Builder builder = new AlertDialog.Builder(activity, AlertDialog.THEME_HOLO_DARK);
                builder.setCancelable(true).setMessage("\n" + message + "\n").setNeutralButton(activity.getResources().getString(R.string.label_ok), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.cancel();

                    }
                }).setOnCancelListener(new DialogInterface.OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface dialog) {
                        // TODO Auto-generated method stub
                        if (shouldFinishActivity)
                            activity.finish();
                    }
                }).show();
            }

        }

//check for connectivity
    public static final boolean isOnline(Context context) {
        NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        return (networkInfo != null && networkInfo.isConnectedOrConnecting());
    }

//for the 'HOME' button on every activity
    public static final void goToDashboard(Activity activity) {
        goToActivity(ActivityDashboard.class, activity);

    }

    }

在我的主要活动中,我这样调用这个函数

In my main activity, I call this function like this

GlobalMethods.showSimpleAlertDialog(this, R.string.error_msg_failed_load, true);

这是一个好方法?这是否导致内存泄漏?如果是,请指导我的最佳实践使用的工具类

Is this a good approach? Does this cause memory leaks? If yes, please guide me on the best practices on using utility classes

推荐答案

没有,这是一个不错的办法。 你最好通的WeakReference<活动> 来你的方法,并实现这样的方法:

No, it's a bad approach. You better pass WeakReference<Activity> to your methods, and implement methods like this:

public static final void showSimpleAlertDialog(final WeakReference<Activity> mReference, String  message, final boolean shouldFinishActivity) {
    Activity activity = mReference.get();
    if (activity != null) {
        //your code goes here
    }

延伸阅读: HTTP://android-developers.blogspot .AE / 2009/01 /避免内存,leaks.html

这篇关于提到过活动的工具类的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆