AsyncTask inside a Static method - Good Coding Practice?

吃可爱长大的小学妹 提交于 2020-01-03 08:33:10

问题


I currently have a helper class to perform rudimentary AsyncTasks such as the following. I call the function from an activity as and when needed. The code seems to work fine and I haven't encountered any problems. But, I was wondering if this is a good coding practice or if there were any ramifications that I am unaware of. Any feedback would be gladly accepted and appreciated.

public class OtherUtils {

    public static void updatePromptsOption(final boolean showPrompt, final Context context) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                Editor preferenceEditor = PreferenceManager.getDefaultSharedPreferences(context).edit();
                preferenceEditor.putBoolean(Constants.SHOW_PROMPT, showPrompt).commit();
                return null;
            }

        }.execute();    
     }
}

回答1:


I don't see anything wrong with doing things that way. Being a static function, you're not hiding implicit this references that could bite you later. Seems like a reasonable convenience function to me.



来源:https://stackoverflow.com/questions/9185117/asynctask-inside-a-static-method-good-coding-practice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!