Wednesday, 15 August 2012

android - How to keep a reference of AsyncTask while orientation changes -



android - How to keep a reference of AsyncTask while orientation changes -

i starting asynctask portrait orientation, changed orientation landscape. in moment need reference of running asynctask can cancel task.

this situation actually:

@override public void onclick(view v) { switch(v.getid()) { case r.id.startbtn: sampletaskone = new sampleasynctaskone(this, sampletaskcallbacks); sampletaskone.execute(); break; case r.id.stopbtn: sampletaskone.cancel(true); sampletaskone = null; default: break; } }

right after orientation changes if click stopbtn nullpointerexception occured because sampletaskone not initialized. how can maintain reference of running asynctask ?

please don't utilize fragment. don't mention duplicate of "how utilize asynctask while orientation changes" or that. know several alternative solutions want maintain reference of asynctask.

you can store sampletaskone static variable , pass null when finished or canceled.

android android-activity android-asynctask

No comments:

Post a Comment