multithreading - How can a background thread modify the UI in between Activity loads in Android? -
i have download process runs in background, , updates ui progress (a listview adapter). works fine until leave activity , come back. after loading activity again there "new" listview object not same 1 bound bg download process. how can structure code background process can talk listview in activity?
the specific line is:
adapter.notifydatasetchanged();
here shell of download class:
public class download { } protected void start() { transfermanager tx = new transfermanager(credentials); this.download = tx.download(s3_bucket, s3_dir + arr_videos.get(position), new_video_file); download.addprogresslistener(new progresslistener() { public void progresschanged(final progressevent pe) { handler.post( new runnable() { @override public void run() { if ( pe.geteventcode() == progressevent.completed_event_code ) { download.this.oncomplete(); } else { download.this.onprogressupdate(); } } }); } }); } protected void onprogressupdate() { this.download_status = "downloading"; double progress = this.download.getprogress().getpercenttransfered(); integer percent = progress.intvalue(); //log.v("runnable", percent + ""); downloaded_data.edit().putint(position+"", percent).commit(); adapter.notifydatasetchanged(); } }
the short answer "no". there's no simple way find/keep reference listview in destroyed/recreated activity.
one way can around use broadcastreceiver. can broadcast progress intents, , have activity register/deregister intents in oncreate() , onpause().
another (arguably easier) hack can persist state (along lines of you're doing downloaded_data.edit()), , have thread in activity regularly polls state , updates listview accordingly.
Comments
Post a Comment