1

I am getting this exception after trying to rotate device:

java.lang.IllegalStateException: Activity has been destroyed.

In my OnCreate() I am doing simple:

FragmentTransaction fragTx = FragmentManager.BeginTransaction();
_myMapFragment = MapFragment.NewInstance(mapOptions);
fragTx.Replace(Resource.Id.map, _myMapFragment, "map");
fragTx.Commit();

and I am also trying to remove mapfragment in my OnDestroy(), like this:

FragmentTransaction fragTx = FragmentManager.BeginTransaction();
fragTx.Remove(_myMapFragment);
fragTx.CommitAllowingStateLoss();

Thanks in advance for any help with this issue.

Here is how my Main.axml looks like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_below="@id/toolbar">
    <AutoCompleteTextView
        android:hint="Nazwa ulicy w Warszawie"
        android:id="@+id/txtTextSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:imeOptions="actionSearch"
        android:imeActionId="@+id/txtTextSearch"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp" />
</LinearLayout>
<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    class="com.google.android.gms.maps.MapFragment" />
<Button
    android:text="Moja lokalizacja"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:id="@+id/btnLocalizeMe"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@android:color/holo_orange_dark" />

And header of my MainActivity:

public class MainActivity : AppCompatActivity, 
    GoogleApiClient.IConnectionCallbacks, 
    GoogleApiClient.IOnConnectionFailedListener, 
    ILocationListener
Tomasz Kowalczyk
  • 1,873
  • 2
  • 23
  • 33

1 Answers1

1

Try to put

<activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize" />

and in Activity

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}
Shekhar
  • 254
  • 2
  • 10
  • Then u have to decrease the size of images only.it is exceeding heap memory.That's error is coming – Shekhar Nov 18 '15 at 10:26
  • This flag is not recommended: http://stackoverflow.com/questions/27396892/androidlargeheap-true-in-mainifast-advantages and I am not using images but Google Map – Tomasz Kowalczyk Nov 18 '15 at 10:27
  • check this once it may usefull http://stackoverflow.com/questions/14800192/illegalstateexception-activity-has-been-destroyed-when-getsupportfragmentman – Shekhar Nov 18 '15 at 10:33
  • this link was posted above by @DmitryArc ;) – Tomasz Kowalczyk Nov 18 '15 at 10:36
  • if it works please edit the comment No, it didn't help – Tomasz Kowalczyk 40 mins ago – Shekhar Nov 18 '15 at 11:05
  • If you copy code **exactly** from other people, even if the code is licensed under creative commons, please **at least** note that you've done so. I notice your second block of code matches **exactly** to code [here on developer.android.com](http://developer.android.com/guide/topics/resources/runtime-changes.html). Unless it's a coincidence, in which case I apologise. – Wai Ha Lee Nov 23 '15 at 13:39