-1

In my android application I have a MainActivity, I trigger another function by clicking on a button:

ImageButton button_1 = (ImageButton) findViewById(R.id.button_trigger);
button_1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        webView = (WebView) findViewById(R.id.webViewLayout);

        MyClass myCls = new MyClass(getApplicationContext());
        myCls.getWebView(authWebView);
    }
});

In the MyClass class I trigger an AsyncTaks via:

RespsonseGetterTask task = new RespsonseGetterTask();
task.execute(parameter);

which then is implenmented here with postExecute:

private class RespsonseGetterTask extends AsyncTask<String, Void, Boolean> {
    @Override
    protected Boolean doInBackground(String... args) {
        IdTokenResponse response;
        try {
            response = HttpUtils.request(args);
        } catch (IOException e) {
            return false;
        }
        return true;
    }

   @Override
   protected void onPostExecute(Boolean wasSuccess) {
       if (wasSuccess) {
           logger.debug("SUCCESSFULL");
           // take actions
       } else {
           logger.debug("Error");
           // take actions on error
       }
   }

I can get a IdTokenResponse object, so that my response variable has valid content. So that response object is within that tast, and my MainActivity is not noticed about the recent result.

How can I signal my MainActivity about the response (IdTokenResponse) object ?

6
  • You can do it by 2 ways first use interface and second you can make myclass as innerclass and then override method from myclass
    – Pankaj
    Dec 2, 2015 at 10:45
  • 1
    Check - But I also think using Async task without its post execute being useful to you is no fun. You should go for a Service rather.
    – Skynet
    Dec 2, 2015 at 10:46
  • You can make ResponseGetterTask as inner class of main activity. So that response can use in it Dec 2, 2015 at 10:46
  • @Srinivasan But the RepsonsegetterTask must be fired in the MyClass. Dec 2, 2015 at 12:43
  • Try to make a return type of method call in both class and async postExecute() Dec 2, 2015 at 13:14

2 Answers 2

1

Return you idTokenResponse instead of a boolean...

private class RespsonseGetterTask extends AsyncTask<String, Void, IdTokenResponse> {
    @Override
    protected IdTokenResponse doInBackground(String... args) {
        IdTokenResponse response = null;
        try {
            response = HttpUtils.request(args);
        } catch (IOException e) {
            response = null;
        }
        return response;
    }

...and use the PostExecute to take an action (in your UI activity) according to the result:

    @Override
    protected void onPostExecute(IdTokenResponse response) {

        if(response != null){
            // Take action when your response is not null
        }
        else{
            // Take action when your response is null
        }

     }
}
3
  • @Igallard I added the onPostExecute as I had it in my code, but even if I'd do it with your return value, how'd MainActivity be noticed? Dec 2, 2015 at 12:39
  • if RespsonseGetterTask is local to MainActivity then RespsonseGetterTask.onPostExecute can call a method MainActivity.showToken(IdTokenResponse response)
    – k3b
    Dec 2, 2015 at 12:50
  • You must change or notify your UI Activity in the postExecute. As k3b suggested having a method in your MainActivity works. For Instance, in the showToken method you can get your webview and update it according to the response you got.
    – lgallard
    Dec 2, 2015 at 14:31
0
private class RespsonseGetterTask extends AsyncTask<String, Void, IdTokenResponse> {
    @Override
    protected IdTokenResponse doInBackground(String... args) {
        IdTokenResponse response = null;
        try {
            response = HttpUtils.request(args);
        } catch (IOException e) {
            response = null;
        }
        return response;
    }

use the above and in the button call use the below:

RespsonseGetterTask task = new RespsonseGetterTask();
task.execute(parameter).get();
if(task!= null){
            // Take action when your response is not }else{
            // Take action when your response is null}

so the above allows you to get back the result to the class in which button was clicked

2
  • 1. The button is pressed in the MainAcitvity. --> 2. This triggers MyClass.doSomething() --> 3. Here is my RespsonseGetterTask task. ;; Myclass in not aware of MainActivity. Dec 2, 2015 at 12:59
  • when you click the button in the main activity it calls the asyn task class which runs the background task and return the result to the main activity which you need to process using the second code and if you want to run the bg task on the ui main thread then use the below one in the doInBackground Function: if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } Dec 2, 2015 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.