-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 ?

Ralf Wickum
  • 2,850
  • 9
  • 55
  • 103
  • 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 02 '15 at 10:45
  • 1
    [Check](http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager) - 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 02 '15 at 10:46
  • You can make **ResponseGetterTask** as inner class of main activity. So that response can use in it – iSrinivasan27 Dec 02 '15 at 10:46
  • @Srinivasan But the RepsonsegetterTask must be fired in the MyClass. – Ralf Wickum Dec 02 '15 at 12:43
  • Try to make a return type of method call in both class and async postExecute() – iSrinivasan27 Dec 02 '15 at 13:14
  • @Skynet Tried that with broadcasting: But what do I do, when the sender is not in an Activity, but rather in the `onPostExecute()` Method of my `MyAsynTask extends AsyncTask` ? Because there **getInstance()** does not accept **this**! – Ralf Wickum Dec 02 '15 at 13:40

2 Answers2

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
        }

     }
}
lgallard
  • 462
  • 3
  • 10
  • @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? – Ralf Wickum Dec 02 '15 at 12:39
  • if `RespsonseGetterTask ` is local to `MainActivity` then `RespsonseGetterTask.onPostExecute` can call a method `MainActivity.showToken(IdTokenResponse response)` – k3b Dec 02 '15 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 02 '15 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

Manoj kumar
  • 450
  • 4
  • 13
  • 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. – Ralf Wickum Dec 02 '15 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); } – Manoj kumar Dec 02 '15 at 13:03