0

I made a fragment and I am showing a progressDialog. When the orientation changes my progress dialog doesn't show again, while I am using a setRetainInstance(true).

Code is

public class FragmentTest  extends Fragment implements OnClickListener
 {

Button proceed;
ProgressDialog pDialog;


@Override
public void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    if(pDialog!=null)
    {
    pDialog.show();
    }

}

class inner extends AsyncTask
{

    @Override
    protected void onPreExecute() 
    {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pDialog.setMessage("Processing...");
        pDialog.setCancelable(false);
        pDialog.setMax(900000000);
        pDialog.show();
    }

    @Override
    protected Object doInBackground(Object... arg0) 
    {
        int k=0;
        for(int i=0;i<10000000;i++)
        {
            pDialog.incrementProgressBy(i);
            for(int j=0;j<10000;j++)
            {
                k=k+j;

            }
        }
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onPostExecute(Object result)
    {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        System.out.println("enter here");
        pDialog.dismiss();
    }



}



@Override
public void onStop() 
{
    // TODO Auto-generated method stub
    super.onStop();
    if (pDialog!=null && pDialog.isShowing()){
        pDialog.dismiss();
    }

}

}

But my progress dialog is not showing. Can any one tell me how to solve the problem?

James Dunn
  • 8,064
  • 13
  • 53
  • 87
Qadeer Hussain
  • 653
  • 2
  • 7
  • 17

1 Answers1

0

You need to create the instance of ProgressDialog before you show.

Best approach is to create a method showProgress

private void showProgress(){
    pDialog = new ProgressDialog(getActivity());
    pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pDialog.setMessage("Processing...");
    pDialog.setCancelable(false);
    pDialog.setMax(900000000);
    pDialog.show();
}

And call it when ever required to show the progress dialog.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    showProgress();
}
Libin
  • 16,967
  • 7
  • 61
  • 83