1

My application crashes whenever my location is changed. Application structure is simple: there is one Activity and a many fragments. Conclusion fragment is carried out as follows:

ItemsFragment itemsFragment = new ItemsFragment () {GroupID = groupID, CurrentGroup = currentGroup};
itemsFragment.RetainInstance = true;
var fragmentManager = SupportFragmentManager.BeginTransaction ();
fragmentManager.Replace (Resource.Id.flContent, (SupportFragment) itemsFragment);
fragmentManager.AddToBackStack (null);
fragmentManager.Commit ();

I use C # Xamarin, but java principle is the same, only slightly different syntax. Help a newbie android-developers to understand the reason.

On the mobile device produces an error:

“Unfortunately App has Stopped”

Where only can be put try... catch and got this error:

Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState
Veer3383
  • 1,785
  • 6
  • 29
  • 49
Igor
  • 41
  • 4
  • Here I have answered the same question : https://stackoverflow.com/questions/39101937/android-java-lang-illegalstateexception-in-onrequestpermissionsresult/39103284#39103284 – Ashish Shukla Sep 02 '16 at 07:54
  • There are tons of those question on stackoverflow, each of them provide lots of information about this issue. – mhenryk Sep 02 '16 at 08:58

2 Answers2

1

The reason is that you are sometimes replacing fragments after your Activity has paused. The easiest way to fix it is to use fragmentManager.CommitAllowingStateLoss();

As @user13 mentioned, it is the easiest, and the worst way.
If you want a good solution, you should check if the Activity state is Ok to replace fragment before actually replacing it

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
0

I suggest you should try with Handler

new Handler().post(new Runnable() {
            public void run() {
                ItemsFragment itemsFragment = new ItemsFragment () {GroupID = groupID, CurrentGroup = currentGroup};
                itemsFragment.RetainInstance = true;
                var fragmentManager = SupportFragmentManager.BeginTransaction ();
                fragmentManager.Replace (Resource.Id.flContent, (SupportFragment) itemsFragment);
                fragmentManager.AddToBackStack (null);
                fragmentManager.Commit ();
            }
        });
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96