I have a problem with the current implementation of my app. I execute an asynctask onActivityCreated but I’m not interested in doing this if the user simply changes the orientation of the phone. What is the proper way of making sure that unnescesarry asynchronous calls aren’t made when the user changes the orientation of the phone?
Here are the two methods onActivityCreated and populateList. populateList is called onPostExecute().
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
userControl = new UserController(getSherlockActivity());
lv = (ListView)getView().findViewById(R.id.callforwardlist);
lv.setTextFilterEnabled(true);
list = new ArrayList<HashMap<String,String>>();
callForwardList = new SimpleAdapter(
getSherlockActivity(),
list,
R.layout.callforward_items,
new String[] { "line1","line2" },
new int[] { R.id.callforward_item_text, R.id.callforward_number } );
new PullInfoTask().execute();
lv.setAdapter( callForwardList );
lv.setOnItemClickListener(new OnItemClickListener() {
// *snip*
});
}
private void populateList()
{
cfinfo = userControl.GetCallForwardInfo();
if(Integer.parseInt( cfinfo[0]) == 1)
addItem("Viderestilling altid", cfinfo[1]);
else
addItem("Viderestilling altid", "Slået fra");
if(Integer.parseInt(cfinfo[2]) == 1)
addItem("Viderestilling ved optaget", cfinfo[3]);
else
addItem("Viderestilling ved optaget", "Slået fra");
if(Integer.parseInt(cfinfo[4]) == 1)
addItem("Viderestilling ved intet svar", cfinfo[5]);
else
addItem("Viderestilling ved intet svar","Slået fra");
// Timeout Item
addItem("Timeout",cfinfo[6]);
callForwardList.notifyDataSetChanged();
}
There is an easy way to stop that using,
android:configChanges="orientation"declare this attribute inManifestfile for thatActivity.Adviceable is that to not to use this and handle orientationChange by yourself in code.
using
onRetainNonConfigurationInstance()for a complete tutorial you can go through this Blog. also read here on developer site.