0

I am working on android application. I need to get the value from onpostexecute method. i.e I need to do some tasks with the return value from onpostexecute method. How can I achieve that? Please help me in this regard.

My code:

boolean val= false;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LongOperation1 op = new LongOperation1();
            op.execute("");
//I want the value here again


}

private class LongOperation1 extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            loadFeed2();
            return "Executed";
        }
            @Override
        protected void onPostExecute(String result) {
            dialog1.dismiss();
            try {
                     dialog.dismiss();
//mystuff
                val = true;


        }
            catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        @Override
        protected void onPreExecute() {
            dialog1 = ProgressDialog.show(Taketest.this, "Please wait...",
                    "Retrieving data ...", true);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }
Amrutha
  • 575
  • 4
  • 9
  • 29
  • call function inside postexecute(), pass the value as parameter to that function – Senthil Jun 22 '13 at 08:07
  • hmm yes i used global variable....in onpostexecute method I changed the value to true. Now I want to use it in oncreate method. How can I get that value? – Amrutha Jun 22 '13 at 08:07
  • dont code anything inside oncreate, create separate function say "init" then call this function from postexecute – Senthil Jun 22 '13 at 08:09
  • 2
    take a look to my answer here: http://stackoverflow.com/questions/16752073/how-do-i-return-a-boolean-from-asynctask – Blackbelt Jun 22 '13 at 08:18
  • 1
    @user1455252 pls upvote the answer in the link suggested by blackbelt. he deleted the answer posted which actually answered your question. instead of returning a boolean value in that case return whatever value you like. – Raghunandan Jun 22 '13 at 08:22
  • @blackbelt why did you delete the answer it actually answered the question? – Raghunandan Jun 22 '13 at 08:23
  • @Raghunandan I dont know If my behaviour fullfil the stackoverflow netiquette – Blackbelt Jun 22 '13 at 08:25
  • @blackbeltHi thank you so much...I am able to get the value using your code – Amrutha Jun 22 '13 at 08:34
  • @blackbelt may be this will help http://meta.stackexchange.com/questions/113385/user-posting-exact-duplicate-answers-to-multiple-questions. – Raghunandan Jun 22 '13 at 08:59
  • @Raghunandan still I am a bit uncertain about it. To me seems a bit boderline. As you point out it is a bit different from the original answer but still it remains the same answer :). May a mods could shed a bit of light :) – Blackbelt Jun 22 '13 at 09:08

1 Answers1

1

u can try something like this, make the boolean val as global then

   Public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LongOperation1 op = new LongOperation1();
    op.execute("");
    // here should not check val is true or not because it will run before ansynch complete
}

public void init()
{
 if(val)
      {

   Log.e("val is","true");
      }

 }

 ...........

    @Override
    protected void onPostExecute(String result) {
        dialog1.dismiss();
        try {
                 dialog.dismiss();
     //mystuff
            val = true;

             init();


    }
Senthil
  • 1,244
  • 10
  • 25