Siva K
Siva K

Reputation: 4968

problem with progress dialog in android

in my app when i click a button i am getting some data from network and i am opening a new activity. At that time i am trying to show a progress bar in my screen. Following is my code

 dialog = new ProgressDialog(SearchPage.this);
  dialog.setMessage("Please wait...");
  dialog.setIndeterminate(true);
  dialog.show();
  new Thread() {
     public void run() {
    try {
        Thread.sleep(300);  
        }
    catch (InterruptedException e) 
           {
           e.printStackTrace();
       }              
        getWebPageContents(url);
            Intent myIntent = new Intent(getBaseContext(), SearchList.class);
       startActivityForResult(myIntent, 0);  
                }
              }.start();

The app is working fine and the progress dialog is also working. Now the problem is when i press the back button from second activity the first activity gets opened and the progress dialog gets started and running continuously without stopping.

It gets stopped if i press the back button. I dont want the progress dialog to be viewed when i returning back to the first activity. How to do this........

Upvotes: 0

Views: 800

Answers (4)

nhaarman
nhaarman

Reputation: 100388

You can dismiss the dialog directly after your call to the Activity by calling

dialog.dismiss();

after

startActivityForResult(myIntent, 0);

If you want to dismiss the dialog only when you've returned from this Activity, you need to make sure dialog is a field, and call dialog.dismiss(); in onActivityResult(You may want to check for dialog != null first).

Upvotes: 1

Kartik Bhatt
Kartik Bhatt

Reputation: 924

Add following code after

dialog.setMessage("Please wait...");
dialog.setCancelable(false);

Upvotes: 0

Walid Hossain
Walid Hossain

Reputation: 2714

Add the following code after

startActivityForResult(myIntent, 0);
dialog.dismiss();

Upvotes: 1

Egor
Egor

Reputation: 40203

Call dialog.dismiss() before starting the new activity. Hope this helps.

Upvotes: 3

Related Questions