1

If I have an AsyncTask as a separate class, and I run the AsyncTask from an Activity, will the AsyncTask hold reference to the Activity , just like if it were an inner class of the later? Or does it solve the issue of memory leak?

Also, will passing the context wrapped up in a WeakReference to the AsyncTask make any change?

BVtp
  • 2,308
  • 2
  • 29
  • 68
  • Yes it will hold the reference to the Activity by passing the `context` of the Activity, similar to inner class. – Arshak Jul 27 '16 at 06:24
  • and what you're passing the context wrapped in WeakReference? – BVtp Jul 27 '16 at 06:25
  • 1
    To avoid memory leak, wrap context in WeakReference. – Akshay Bhat 'AB' Jul 27 '16 at 06:28
  • So basically we have two solutions for the memory leak , right? The first one being a static inner class, and the second is a separate class with a WeakReference passed to it – BVtp Jul 27 '16 at 06:29
  • Refer [this](http://stackoverflow.com/a/4220746/5744335) link for better understanding @BVtp – Arshak Jul 27 '16 at 07:05
  • just pass the reference of your activity when asyntask call. – Nirmit Jul 27 '16 at 08:31
  • @Nirmit , passing reference regularly you mean? this will cause memory leak. WeakReference is a different matter though. – BVtp Jul 27 '16 at 08:33
  • when ever you call asyntask you must pass the refrense of activity like this " new Mc_SearchPeopleAroundMeContactGetTask(getActivity()).execute(); " – Nirmit Jul 27 '16 at 08:45
  • well then the memory leak issue remains in that case. It will still hold reference to the activity , not allowing the GC to clean the activity from memory. – BVtp Jul 27 '16 at 08:49

1 Answers1

-1

you can use like this and get the refrense of activity

public class DeleteUserTask extends AsyncTask<String, String, String> {
    private Context context;
    private JSONObject Object, Object1;
    public static String Success, Exception;
    public static boolean isClick;

    public DeleteUserTask(Context ctx) {
        context = ctx;
    }

    @Override
    protected void onPreExecute() {
        OrganizationUser_EditProfileScreen.pb.setVisibility(View.VISIBLE);
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            ActivityBase.result = null;
            Log.e("", "Request-->" + writeJSON().toString());
            ActivityBase.PostMethod(context,
                    WebServiceLinks.UserStatusButtonTask, writeJSON());
            Object = new JSONObject(ActivityBase.result);
            Object1 = Object
                    .getJSONObject("OrganizationUserProfileButtonUpdateResult");
            Success = Object1.getString("Success");
            Exception = Object1.getString("Exception");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        try {
            OrganizationUser_EditProfileScreen.pb.setVisibility(View.GONE);
            if (ActivityBase.result != null && Success == "true") {
                ActivityBase
                        .GeneralDialog(R.string.account, Exception, context);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JSONObject writeJSON() {
        JSONObject object = new JSONObject();
        try {

            object.put("OrgID", OrganizationLoginTask.OrgID);
            object.put("OrgUserID", Search_Screen.OrgUserID);
            object.put("Status", OrganizationUser_EditProfileScreen.Status);
            object.put("ModifiedByTypeCode", "2");

            object.put("ModifiedSourceCode", "8");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }
}
Nirmit
  • 189
  • 8