Passing reference to activity to utility class android

后端 未结 1 623
面向向阳花
面向向阳花 2021-02-06 03:08

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 comm

相关标签:
1条回答
  • 2021-02-06 03:29

    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
        }
    

    Further reading: http://android-developers.blogspot.ae/2009/01/avoiding-memory-leaks.html

    0 讨论(0)
提交回复
热议问题