java - Cancel asynctask launched with onClickListener in an Adapter created inside onPostExecute -
this context: have activity downloads list of video , set them adapter. when user clicks on each item, application downloads , streams video in external application. so, let's see code. main activity has this:
new tvasynctask(getactivity(), gridview, progressbar).execute();
tvasynctask download video list , sets here:
protected void onpostexecute(arraylist<videopreviewdata> list) { progressbar.setvisibility(view.invisible); final tvlistadapter adapter = new tvlistadapter(activity.getapplicationcontext(), r.layout.item_video_list, list); videogridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, final view view, int position, long id) { string videourl = adapter.getitem(position).getlink(); //launch asynctask download video load external media player new getvideoasynctask(activity).execute(videourl); } }); if (videogridview!=null) videogridview.setadapter(adapter); }
getvideoasynctask load video link , lauch intent in external application stream video. in order this, during onloading set progressdialog shows user loading message. after loading completed, intent launched.
problem when progressdialog shown , user click "back button" dialog cancelled asynctask continues work. when finishes launches intent. want prevent this. have onpause method in mainactivity, don't have reference of getvideoasynctask launched adapter. cannot call method asynctask.cancel(true) in order deny intent launch. suggestion?
create object of async task class;
tvasynctask tvtask = new tvasynctask(getactivity(), gridview, progressbar); tvtask.execute();
then in tvasynctask class override oncanceled function
@override protected void oncancelled() { super.oncancelled(); }
in button code check:
if(tvtask != null) tvtask.cancel(true);
Comments
Post a Comment