Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SupportMapFragment get recreated on orientation change

I'm creating a simple app that use google maps in a tab and something else in other. The problem is that the map gets recreated when i change orientation so i goes to latlng 0,0

Here is my Code

MainAcitivy.java

private static final String MAP_FRAGMENT_TAG = "map";
private static final String DUMMY_FRAGMENT_TAG = "DUMMY";

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

    // Set up the action bar to show tabs.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    //
    // // For each of the sections in the app, add a tab to the action bar.
    actionBar.addTab(actionBar
        .newTab()
            .setText(R.string.title_section1)
                .setTabListener(
                    new TabListener<SupportMapFragment>(this,
                        MAP_FRAGMENT_TAG, SupportMapFragment.class)));
    actionBar
        .addTab(actionBar
            .newTab()
                .setText(R.string.title_section2)
                    .setTabListener(
                        new TabListener<DummySectionFragment>(this,
                            DUMMY_FRAGMENT_TAG,
                                DummySectionFragment.class)));

}

TabListener.java

public class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    private final SherlockFragmentActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    public TabListener(SherlockFragmentActivity activity, String tag,
    Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if (mFragment == null) {
            mFragment = mActivity.getSupportFragmentManager()
                .findFragmentByTag(mTag);
        }
        if (mFragment == null) {
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}

Portrait oriented near Congo

Landscape oriented at latlng(0,0)

like image 288
Alvaro Ardila Avatar asked Dec 20 '12 22:12

Alvaro Ardila


People also ask

How to retain data on orientation change?

Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.

What is MapFragment in Android?

public class MapFragment extends Fragment. A Map component in an app. This fragment is the simplest way to place a map in an application. It's a wrapper around a view of a map to automatically handle the necessary life cycle needs.


2 Answers

use

setRetainInstance(true);

in the onCreate method of your MapFragment

like image 133
dumazy Avatar answered Nov 15 '22 05:11

dumazy


I was facing the same problem and this question & even its answer helped me.

But exactly I was having problem with:

  • Google Map (Issue: Clears / Refreshes)
  • ProgressDialog running with AsyncTask (Issue: Crashes)
  • Dialog (Issue: Cancels)

So to go through all these issues, I have added the below code with every activity.

<activity
            android:name="ACTIVITY"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:launchMode="standard"
            android:theme="@android:style/Theme.NoTitleBar"
            android:windowSoftInputMode="stateHidden|adjustPan" />

It worked for me, so though it'll help other too as it's a common issue.

like image 35
Harpreet Avatar answered Nov 15 '22 06:11

Harpreet