Search code examples
androidillegalstateexception

IllegalStateException: Can not perform this action after onSaveInstanceState - after screen rotating


I'm also getting it in a precise context and the solution given here (IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager) don't work.

Here is the code: Should be a working code for test; I hope.

MainActivity.java

public class MainActivity extends FragmentActivity {
  final static int INIT_NETWORK_DONE = 1;
  final static int EXIT_APPLICATION = -1;

  private Site site = new Site(this);
  private WifiManager wifi = null;
  Handler mHandler = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    site.setUrls();

    if (savedInstanceState == null) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        fragmentTransaction.replace(R.id.frame_container, new Fragment_init(site)).commit();

    }
  }
   . . .
  @Override
  public void onSaveInstanceState(Bundle saveInstanceState) {

       //super.onSaveInstanceState(saveInstanceState);
  }
}

Fragment_init.java

public class Fragment_init extends Fragment {
    Fragment fragment = null;
    private InitTask mInitTask = null;
    // Taille maximale du téléchargement
    public final static int MAX_SIZE = 100;
    // Identifiant de la boîte de dialogue
    public final static int ID_DIALOG = 0;
    public final static int DO_INIT_WIFI = 1;

    private Site site = null;


    public Fragment_init() {
    }    

    public Fragment_init(Site _site) {
        site = _site;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_init, container, false);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (savedInstanceState == null) {
            Animation animation = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.animator.welcome_anim);
            ImageView logoSite = (ImageView)getActivity().findViewById(R.id.imageAvenArmand);
            logoSite.startAnimation(animation);

                // Do the init
                mInitTask = new InitTask(Fragment_init.this, site, getFragmentManager());
                // On l'exécute
                mInitTask.execute(0);
        }
    }

    // L'AsyncTask est bien une classe interne statique
    static class InitTask extends AsyncTask<Integer, Integer, Integer> {
        // Référence faible à l'activité
        private Fragment_init mActivity = null;
        private Site site = null;
        Context context = null;
        private FragmentManager fragmentManager = null;

        public InitTask (Fragment_init pActivity, Site pSite, FragmentManager _fragmentManager) {
            mActivity = pActivity;
            context = mActivity.getActivity();
            site = pSite;
            fragmentManager = _fragmentManager;
          }

        @Override
        protected void onPreExecute () {
        }

        @Override
        protected void onPostExecute (Integer result) {
            if(result != 1) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(mActivity.getActivity());
                alertDialog.setTitle(R.string.label_titleAlertInit);
            } else {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                fragmentTransaction.replace(R.id.frame_container, new Fragment_selectLanguage(site)).commitAllowingStateLoss();
            }
        }

        @Override
        protected Integer doInBackground (Integer... arg0) {
            URL url = null;
            BufferedInputStream buf;
            ArrayList<Language> languages = null;

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {

            }
            return 1;
        }

        @Override
        protected void onProgressUpdate (Integer... prog) {
        }

        @Override
        protected void onCancelled () {
        }

        private int processStream(InputStream inputStream) {
            // Création du parser XML
            XmlPullParserFactory factory;
            int lineNumber = 0;

            return (1);

        }
    }

    @Override
    public void onSaveInstanceState(Bundle saveInstanceState) {
        //super.onSaveInstanceState(saveInstanceState);

    }
}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:screenOrientation="portrait"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.cabbonline.ndguidelt.MainActivity" >
</FrameLayout>

fragment_init.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragmentInit"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="com.cabbonline.ndguidelt.MainActivity" >

   <ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:src="@drawable/launcher_icon" />

</RelativeLayout>

Anyway, I think that not calling super.onSaveInstanceState() should cause problem on context saving no?

so if you rotate the screen when the image is fading, you should get IllegalStateException on call on commit()

So my workaround is to prevent the screen rotation during this transitional screen. Ok that's ok for me but I doubt it could be an answer for most of you. anyway, it could help.

So I call this in onCreateView() in fragment_init().

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

And I then call this in onCreateView() in the next fragment:

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

:-/

Any other idea?


Solution

  • So I solved my problem using the wonderfull message handler implementation explained here: How to handle Handler messages when activity/fragment is paused

    Thx to Akagami which pointed me on the post.

    Regards,