1

I'm trying to dismiss dialog in AsyncTask's onPostExecute() and also set text in textview, but textview doesn't change and I get the "Window already focused, ignoring focus gain of...".

Here's the code:

    protected void onPreExecute() {
    dialog = ProgressDialog.show(mContext, "","Loading...", true);

}

protected void onPostExecute(Object result) {
    dialog.dismiss();
    tv.setText("some text");

}

Progress dialog is shown and when my background work is complete, it is dismissed but there's no change to textview. Without progress dialog, text view is updated.

Any idea, solution to this problem?

Thanks!

Swayam
  • 16,294
  • 14
  • 64
  • 102
newman555p
  • 181
  • 1
  • 4
  • 13
  • Have you tried putting dialog.dismiss() below your tv.setText() line? – James Fazio Aug 31 '12 at 16:46
  • There are a lot of similar questions to this. [This person said his worked as you have after he ran clean project](http://stackoverflow.com/q/11680631/1134705). – jnthnjns Aug 31 '12 at 16:49
  • [Check this one out as well](http://stackoverflow.com/a/9671602/1134705) – jnthnjns Aug 31 '12 at 16:50
  • Yes, I've read them before. Maybe I missed something, but there's no example with simultaneous dimiss of dialog and text update (or anything similar). I think that's the problem here...As I said, without the dialog, everything works just fine. – newman555p Aug 31 '12 at 16:57

2 Answers2

2

Try this code snippet after you have called dialog.dismiss().

         if (!dialog.isShowing()) 
            {
                TextView tv = (TextView) findViewById(R.id.textView);
                tv.setText("some text");
            }
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • OK, I just realized that even without your solution my "some text" that is actually result from doInBackground(), textview is updated :( When I use my actual data that I process in doInBackground(), it doesn't work. As I mentioned, withou dialog, everything works great, so I have another problem. Thank you anyway for your effort. – newman555p Aug 31 '12 at 17:06
  • **"When I use my actual data that I process in doInBackground(), it doesn't work"**, could you explain this more ? And what problem are you facing right now ? – Swayam Aug 31 '12 at 17:09
  • Sorry, I was unclear. I tried my original code with actually using "some text" and "some text" is shown in text view. But, when I use a string that I get as a result from doInBackground(), that string isn't shown in text view and I tried this with and without your code. – newman555p Aug 31 '12 at 17:12
  • 1
    Oh, that is sad. But that makes me believe that there must be some other problem, maybe with the string you are handling inside doInBackground(). I suggest you make a new post regarding only that string handling part. And also try giving a Toast to check if your string is being generated properly. Good luck mate! – Swayam Aug 31 '12 at 17:20
0
public class MyActivity extends Activity 
     {
/**
 * Called when the activity is first created.
 */
ProgressBar progressBar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    progressBar = (ProgressBar) findViewById(R.id.pb);
    TextView view=(TextView) findViewById(R.id.myt);
    new Task(view).execute();


}

by passing TextView instance in constructor we can use in progressbar

private class Task extends AsyncTask<Void, Integer, Void> {
    TextView view;
    private Task(TextView textView) {
        this.view= textView;
    }

    int myProgress;

    @Override
    protected void onPreExecute() {
        myProgress = 0;
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        Toast.makeText(MyActivity.this,
                "onPostExecute", Toast.LENGTH_LONG).show();

        super.onPostExecute(aVoid);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {

        progressBar.setProgress(values[0]);
        view.setText("Value"+myProgress);
        super.onProgressUpdate(values);
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();    //To change body of overridden methods use File | Settings | File Templates.
    }


    @Override
    protected Void doInBackground(Void... voids) {
        while (myProgress < 100) {
            myProgress++;

            publishProgress(myProgress);
            SystemClock.sleep(100);
        }
        return null;
    }
}

  }
Rinkesh
  • 3,150
  • 28
  • 32