片段实例保留在android中

时间:2014-04-30 16:20:23

标签: android

在main activiy我调用一个片段,在片段iam中显示异步任务中的进度对话框现在我的问题是当方向改变我的进度对话框重新启动时我希望它使用retainInstance(true)保持其状态iam但它不工作。  我的片段代码是

@Override
public void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);
            setRetainInstance(true);

    if(savedInstanceState!=null)
    {
        if(pDialog!=null)
        {
            pDialog.show();
        }

    }


}
@Override
public void onSaveInstanceState(Bundle outState)
{
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    outState.putString("status", "ok");

}


public void showProgress()
{

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


public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
    View view=inflater.inflate(R.layout.fragment, container, false);
    proceed=(Button) view.findViewById(R.id.b1);
    proceed.setOnClickListener(this);
    return  view;

}



@Override
public void onClick(View v) 
{

    new inner().execute();
}
class inner extends AsyncTask
{

    @Override
    protected void onPreExecute() 
    {
        // TODO Auto-generated method stub
        super.onPreExecute();
        showProgress();

    }

    @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 onPause() 
{
    // TODO Auto-generated method stub
    super.onPause();
}
@Override
public void onStop() 
{
    // TODO Auto-generated method stub
    super.onStop();
    if (pDialog!=null && pDialog.isShowing()){
        pDialog.dismiss();
    }

}
@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);

}

2 个答案:

答案 0 :(得分:0)

您必须在onDetach中保存当前进度并在onAttach中再次使用它。例如:

    private ProgressDialog buildDialog() {
    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setCancelable(false);
    progressDialog.setTitle(getResources().getString(R.string.please_wait));
    progressDialog.setIndeterminate(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setProgress(currentProgress);
    progressDialog.setMax(100);
    return progressDialog;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    attached = true;
    if (taskStarted) {
        progressDialog = this.buildDialog();
        progressDialog.setMessage(getResources().getString(messageId));
        progressDialog.show();
    }
}

@Override
public void onDetach() {
    super.onDetach();
    if (progressDialog != null && progressDialog.isShowing()) {
        currentProgress = progressDialog.getProgress();
        progressDialog.dismiss();
        progressDialog = null;
    }
    attached = false;
}

答案 1 :(得分:0)

前几天我写了一个这样的例子。你去吧

https://gist.github.com/slightfoot/11368894