I'm facing problem with line of code:
mt=(MyTask)getLastNonConfigurationInstance();
mt is instance of MyTask class
static class MyTask extends AsyncTask<String,Integer,Void>
{
MainActivity activity;
void link (MainActivity act)
{
activity=act;
}
void unlink()
{
activity=null;
}
@Override
protected Void doInBackground(String... params) {
try{
for(int i=1;i<=10;i++){
TimeUnit.SECONDS.sleep(1);
publishProgress(i);
Log.d("qwe", "i = " + i
+ ", MyTask: " + this.hashCode()
+ ", MainActivity: " + activity.hashCode());
}
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
activity.tv.setText("i = "+values[0]);
}
}
and i save it in onRetainCustomNonConfigurationInstance method
@Override
public Object onRetainCustomNonConfigurationInstance() {
mt.unlink();
return mt;
}
Error Arrives when I try to pass value from configuration instance to mt, error message is
java.lang.ClassCastException: android.support.v4.app.FragmentActivity$NonConfigurationInstances cannot be cast to com.example.bermud06.p091_asynctaskdisplayrotate.MainActivity$MyTask
Using nexus 5 device with API Level 22.
Use getLastCustomNonConfigurationInstance()
instead. From the Docs for FragmentActivity
:
public Object onRetainCustomNonConfigurationInstance()
Use this instead of
onRetainNonConfigurationInstance()
. Retrieve later withgetLastCustomNonConfigurationInstance()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.