1

I am using Youtube Extractor library

implementation 'com.github.HaarigerHarald:android-youtubeExtractor:master-SNAPSHOT'

I am using this to fetch YouTube download URL in my fragment which upload to my firebase database as soon as it finish extracting

if (requestCode==0000 && resultCode==getActivity().RESULT_OK){

            if (videoPath != null ) {
                for (String s : videoPath) {

                    new YouTubeExtractor(context) { // THIS LIBRARY SHOWING WARNING
                        @Override
                        protected void onExtractionComplete(SparseArray<YtFile> ytFiles, VideoMeta videoMeta) {
                            if (ytFiles !=null){
                                int itag = 18;
                                String link = ytFiles.get(18).getUrl();

                                Map<String, Object> map = new HashMap<>();
                                map.put("Message", link);

                                String Temp_Key = databaseReference.push().getKey();
                                Map<String, Object> RoomKey = new HashMap<>();
                                databaseReference.updateChildren(RoomKey);
                                DatabaseReference message_Root = databaseReference.child(Temp_Key);
                                message_Root.updateChildren(map);

                            }
                        }

                    }.extract(s, true, true);
                
                }
            }

My question is how to solve this warning with this library. I don't want to use @SuppressLint("StaticFieldLeak") which only suppress warning not resolve it.

Zain
  • 37,492
  • 7
  • 60
  • 84
Ritu
  • 518
  • 2
  • 12
  • 35
  • Well, if AsyncTask is not declared static, it may lead to leaks because it is not Lifecycle aware. So for example when you rotate your device, if you dont manually destroy the task, it may keep reference to destroyed objects and therefore cause leaks. If you want to fix this you need to make AsyncTask static. – tendai Apr 13 '19 at 00:40
  • @T.Mas As i am using this library i am not able to provide it `WeakReference`. and also can't modified library method. Any solution – Ritu Apr 13 '19 at 16:21
  • Does this answer your question? [Warning: This AsyncTask class should be static or leaks might occur](https://stackoverflow.com/questions/44309241/warning-this-asynctask-class-should-be-static-or-leaks-might-occur) – auspicious99 Feb 05 '22 at 04:45

1 Answers1

2

To avoid memory leak, you need to create a week reference to the activity/fragment within the AsyncTask; and here is a full explanation for this with below code sample

public class MyActivity extends AppCompatActivity {

int mSomeMemberVariable = 123;

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

    // start the AsyncTask, passing the Activity context
    // in to a custom constructor 
    new MyTask(this).execute();
}

private static class MyTask extends AsyncTask<Void, Void, String> {

    private WeakReference<MyActivity> activityReference;

    // only retain a weak reference to the activity 
    MyTask(MyActivity context) {
        activityReference = new WeakReference<>(context);
    }

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

        // do some long running task...

        return "task finished";
    }

    @Override
    protected void onPostExecute(String result) {

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

        // modify the activity's UI
        TextView textView = activity.findViewById(R.id.textview);
        textView.setText(result);

        // access Activity member variables
        activity.mSomeMemberVariable = 321;
    }
}

}

Also there are other alternatives to the AsyncTask like Executor and AsyncTaskLoader that you may think of them instead. Hope this helps.

Zain
  • 37,492
  • 7
  • 60
  • 84
  • Thanks for the answer but i cant modified library and also i tried to assign weakreference but its not working at all – Ritu Apr 13 '19 at 16:23
  • @Ritu; I just come across this problem in own project; just solved it by isolating the AsyncTask out of the activity and utilized the MVVM pattern with architecture components (Live data); I put the custom AsycnTask into the Repository. and then just made it a static, and all set... so try to extract any code that updates UI from asynctask, and just make it grabs API data; and the LiveData will make the rest – Zain Apr 24 '19 at 13:04