gpt4 book ai didi

android - SupportMapFragment 在方向更改时重新创建

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:00 26 4
gpt4 key购买 nike

我正在创建一个简单的应用程序,它在选项卡中使用谷歌地图,在其他选项卡中使用其他内容。问题是当我改变方向时 map 会重新创建,所以我转到 latlng 0,0

这是我的代码

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)

最佳答案

使用

setRetainInstance(true);

在 MapFragment 的 onCreate 方法中

关于android - SupportMapFragment 在方向更改时重新创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13981259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com