0

I have a question regarding this simple frequently occurring situation in android .

I have an activity that will invoke the async task and async task will draw values from SQLite database and update on the UI. I used Async task to make the UI reponsive and fast.

This is the code I have been working on.

SqlHandler sqlHandler;
@BindView(R.id.list) ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity1);

    ButterKnife.bind(this);

    sqlHandler = new SqlHandler(this);

    new DisplayAll(this).execute();

    listView.setOnItemClickListener((AdapterView<?> parent, View view,
                                    int position, long id) -> {
                Intent i = new Intent(getApplicationContext(), Activity2.class);
                String text = textView.getText().toString();
                startActivity(i);
    });
}

private class DisplayAll extends AsyncTask<Void, Void, Void> {

    int null_val;

    final ArrayList<listRow=> myList = new ArrayList<>();

    private WeakReference<Activity> mActivity;

    public DisplayAll(Activity activity) {
        mActivity = new WeakReference<>(activity);
    }


    @Override
    protected Void doInBackground(Void... params) {

        myList.clear();
        String query = " ...";

        Cursor c1 =sqlHandler.selectQuery(query);
        if (c1 != null && c1.getCount() != 0) {
            if (c1.moveToFirst()) {
                do {
                     .....

                } while (c1.moveToNext());
            }
        }

        try {
            null_val = Objects.requireNonNull(c1).getCount();
            c1.close();
        }
        catch (NullPointerException e)
        {
            Log.e("NPE", "" + e);
         }
        return null;
    }
    @Override
    protected void onPostExecute(Void param) {

        // get a reference to the activity if it is still there
        Activity activity = mActivity.get();
        if (activity == null || activity.isFinishing()) return;

        ProgressBar prgBar=findViewById(R.id.prgbar);
        listAdapter Adapter;

        prgBar.setVisibility(View.GONE);
        Adapter = new listAdapter(getApplicationContext(), myList);
        listView.setAdapter(Adapter);

    }
}

I had checked this question also.I have added the weak reference to my class now. But still Android Studio warns me about the memory leak.

I tried to change it to static, but changing the sqlhandler as static also causes memory leak. To change the async task to a top-level class is not good for me. I have many async tasks in different activities.

So anyone have any idea how to tackle this?

Jayan Dev
  • 67
  • 1
  • 10
  • Does this answer your question? [Android Asyntask: Use weak reference for context to avoid device rotate screen](https://stackoverflow.com/questions/9809336/android-asyntask-use-weak-reference-for-context-to-avoid-device-rotate-screen) – Saswata Jan 04 '20 at 10:33
  • @Saswata Actually I am not using any context in the asynctask. I tried weakreference. You can see in the code. But it still says chances of memory leak. – Jayan Dev Jan 04 '20 at 11:09
  • Hi, you are using an activity which contains the context which will arise a memory leak warning. – Saswata Jan 05 '20 at 07:35

0 Answers0