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
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)));
}
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) {
}
}
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.
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.
use
setRetainInstance(true);
in the onCreate method of your MapFragment
I was facing the same problem and this question & even its answer helped me.
But exactly I was having problem with:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With