0

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;
}`
Community
  • 1
  • 1
m27
  • 13
  • 4
  • please post some code – Anirudh Sharma May 12 '15 at 08:46
  • You should some thing like RoboSpice or Datadroid(deprecated) if you want to procced even if activity lost – Nik Myers May 12 '15 at 08:47
  • 1
    Looks like some context is missing from your question. Why all members of activity become null? Do you have activity recreated by, say, rotating the device? – Haspemulator May 12 '15 at 08:47
  • Give some code about your activity, listener and Asynctask class. – ondermerol May 12 '15 at 08:54
  • So which variables are null and what happens with the device in the meanwhile? – Haspemulator May 12 '15 at 08:58
  • @Haspemulator nothing happens to to device and variable become null in displayView() , listView becomes null and setTitle(getResources().getString(R.string.wealth)); also giving NUllPointerException – m27 May 12 '15 at 09:01
  • in displayView(), setTitle(getResources().getString(R.string.wealth)); giving NullPointerException and listView also became null. @Haspemulator – m27 May 12 '15 at 09:04
  • Please add also the code where you start the AsyncTask. – Haspemulator May 12 '15 at 09:04
  • @NikMyers sorry i didn't get you , Can you please elaborate your answer – m27 May 12 '15 at 09:05
  • Ig your context at the time of finishing task is null, you should consider to use libraries for async operations, which have their own lifecycle, see robospice for example : https://github.com/stephanenicolas/robospice and a video which tells you how it works and why you should consider to use it https://vimeo.com/66201359 – Nik Myers May 12 '15 at 09:08
  • have you tried using interfaces to communicate activities with fragments? – Tofasio May 12 '15 at 09:10
  • Okay, now I'm almost sure where the problem is. But to be 100% sure, could you also add the code for the `AsyncCallWS` class? – Haspemulator May 12 '15 at 09:13
  • What is this "new MainActivity()"? – ondermerol May 12 '15 at 09:14
  • @Haspemulator do we think the same problem :) – ondermerol May 12 '15 at 09:15
  • If you go through the following link's http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a @HelmiB's answer then if u read my question you can understand my problem more clearly – m27 May 12 '15 at 09:20
  • @ondermerol new MainActivity(), is reference of the Activity class which is passed as a constructor parameter to Asynctask that will be used to call the processFinish() method of interface class which is implemented by MainActiivty() – m27 May 12 '15 at 09:24
  • It passes a new reference of the newly created MainActivity object. @Haspemulator answer explains it and also it is related with your problem. – ondermerol May 12 '15 at 10:48

1 Answers1

0

Well, your problem is in this line of code:

asyncCallWS = new AsyncCallWS(MainActivity.this, input, position,new MainActivity());

Or, particularly, in the statement new MainActivity(). You create a new instance of MainActivity class here and then you use it as a callback. Obviously, it will have all the fields non-initialized. Use MainActivity.this instead of new MainActivity(). And please remember that Android manages all of your activities. You never want to create one yourself.

Haspemulator
  • 11,050
  • 9
  • 49
  • 76
  • hi, i updated the Asynctask's constructor , and mean while i will try your solution also – m27 May 12 '15 at 09:29