How to return a value from Asynctask (Different class) to activity back from which you called the Asynctask , here i have followed the intsruction given in following link How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? @HelmiB's
I have done everything and its returning the result to activity's method processFinish() also , the problem is lost the activity control or focus, i could not do further actions using the Asynctask's result, as because all my activity's members becomes null.
How to proceed ?
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog != null)
{
pDialog.cancel();
}
if (StringUtil.hasValue(responseXmlString))
{
if (Integer.parseInt(AppUtil.getXpathValue("Result/ErrorNo",AppUtil.buildDocument(responseXmlString))) == 0)
{
asyncResponse.processFinish(responseXmlString);
}
}
@Override
public void processFinish(Object output)
{
Log.d("Response From Asynchronous task:", (String) output);
displayView(position,(String) output);
}
private void displayView(int position,String responseXml)
{
// update the main content by replacing fragments
boolean isFragment = true;
Fragment fragment = null;
/*//For Handling back press
if (getIntent().getBooleanExtra("FromPassBook", false) )
{
getIntent().putExtra("FromPassBook", false);
position = 7;
}
if (getIntent().getBooleanExtra("toCustomerAcocunts", false) )
{
getIntent().putExtra("toCustomerAcocunts", false);
position = 1;
}*/
switch (position)
{
case 0:
fragment = new ChartFragment();
setTitle(getResources().getString(R.string.wealth));
break;
default:
break;
}
if (fragment != null && isFragment)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft =fragmentManager.beginTransaction();
fragmentStack.push(fragment);
//passing data to fragment
if (StringUtil.hasValue(responseXml))
{
Bundle bundle = new Bundle();
bundle.putString("responseXml", responseXml);
fragment.setArguments(bundle);
}
ft.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
listView.setItemChecked(position, true);
listView.setSelection(position);
mDrawerLayout.closeDrawer(listView);
}
else
{
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
private void callService(int position)
{
String input = "";
AsyncCallWS asyncCallWS;
switch (position)
{
case 0:
input = "<Parm><ProcessID>101</ProcessID><MobileNo>" + mobileNumber + "</MobileNo></Parm>";
asyncCallWS = new AsyncCallWS(MainActivity.this, input, position,new MainActivity());
asyncCallWS.execute();
break;
}
Asynctask class constructor
`AsyncResponse asyncResponse;
public AsyncCallWS(Context context,String input,int position,AsyncResponse response )
{
this.context = context;
this.inputToservice = input;
this.position = position;
asyncResponse = response;
}`