0

This is sample programme for Progress Dialogue Fragment ..normally it work fine but orientation changed it crashes..

with this error

java.lang.IllegalSt    ateException: Can not perform this action after onSaveInstanceSt    ate
at android.support.v4.app.FragmentManagerImpl.checkSt    ateLoss(FragmentManager.java:1375)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1393)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:636)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:615)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:138)
at com.inntot.langtest.MainActivity$LongOper    ation.onPostExecute(MainActivity.java:75)
at com.inntot.langtest.MainActivity$LongOper    ation.onPostExecute(MainActivity.java:57)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.disp    atchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(N    ative Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)

Main Activity

public class MainActivity extends ActionBarActivity 
{
Button change;
ProgressDialogFragment p;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    change = (Button)findViewById(R.id.buttonchange);
    change.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            p = ProgressDialogFragment.newInstance("Hai","Hello");
            p.show(getSupportFragmentManager(),"update");
            new LongOperation().execute("");

        }
    });

}

private class LongOperation extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.interrupted();
            }
        }
        return "Executed";
    }

    @Override
    protected void onPostExecute(String result) {
      p.dismissDialog();
        p = ProgressDialogFragment.newInstance("Hai","Home");
        p.show(getSupportFragmentManager(),"update");

    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}
}

Progress Dialogue fragment

 public class ProgressDialogFragment extends DialogFragment
{
String Title,Message;
static ProgressDialogFragment newInstance(String Title,String Message){
    ProgressDialogFragment PDF= new ProgressDialogFragment();
    Bundle bundle = new Bundle();
    bundle.putString("Title",Title);
    bundle.putString("Message",Message);
    PDF.setArguments(bundle);
    return  PDF;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setCancelable(false);
    setRetainInstance(true);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    ProgressDialog dialog = new ProgressDialog(getActivity(), getTheme());
    dialog.setTitle(getArguments().getString("Title"));
    dialog.setMessage(getArguments().getString("Message"));
    dialog.setIndeterminate(true);
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    return dialog;
}
public void dismissDialog()
{
    dismiss();
}
@Override
public void onDestroyView() {
    if (getDialog() != null && getRetainInstance())
        getDialog().setOnDismissListener(null);
    super.onDestroyView();
}
}

Can anyone help me fix the problem...

Boban
  • 41
  • 1
  • 8
  • Can you post the full stacktrace? – Ganesh Kumar Aug 06 '15 at 07:51
  • error log just updated – Boban Aug 06 '15 at 08:20
  • Try this one in Manifest file `` – TechArcSri Aug 06 '15 at 08:25
  • Have you checked these posts? http://stackoverflow.com/questions/7575921/illegalstateexception-can-not-perform-this-action-after-onsaveinstancestate-h http://stackoverflow.com/questions/7469082/getting-exception-illegalstateexception-can-not-perform-this-action-after-onsa – Ganesh Kumar Aug 06 '15 at 08:50
  • Is this correct Way for handling this Error? – Boban Aug 06 '15 at 08:50
  • On these restart everything...from beginning...android-mantra suggestion is working... but i don't know its proper way of handling rotation . i think activity not destroying and recreating as it suppose to.. – Boban Aug 06 '15 at 08:52
  • @android-mantra, how does the attribute `android:configChanges' help here when android doc suggests not to use that attribute unless there is no other option? – Ganesh Kumar Aug 06 '15 at 08:54
  • @GaneshKumar its very simple thing because when the orientation changes it looses all the instances of the dialogfragment and recreates all the thing. The older instances gets call where its already destroyed and causes the crash of app – TechArcSri Aug 06 '15 at 09:54
  • @ android-mantra is there any other method to handle this.. – Boban Aug 06 '15 at 10:14

0 Answers0