Reputation: 372
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) {
}
}
Upvotes: 6
Views: 3518
Reputation: 14435
use
setRetainInstance(true);
in the onCreate method of your MapFragment
Upvotes: 11
Reputation: 3070
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.
Upvotes: 3