我有一个TabLayout和ViewPager的活动。每个Tab表示一个片段。我在片段中实现了SKMapViewHolder(skobbler),它运行正常。但每次我旋转屏幕时,都会重新创建MapView。我该如何保留它?
这里的代码:
fragment_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.skobbler.ngx.map.SKMapViewHolder
android:id="@+id/map_surface_holder"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
片段:
public class NavigationFragment extends Fragment {
private final static String TAG=NavigationFragment.class.getSimpleName();
private SKMapSurfaceView mapView;
private SKMapViewHolder mapHolder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setRetainInstance(true);
Log.i(TAG, "onCreate executed");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "onCreateView executed");
View view = inflater.inflate(R.layout.fragment_navigation, container, false);
mapHolder = (SKMapViewHolder) view.findViewById(R.id.map_surface_holder);
SKMapSurfaceView mapView = mapHolder.getMapSurfaceView();
MapSurfaceListener mapSurfaceListener = new MapSurfaceListener(this.mapView);
mapHolder.setMapSurfaceListener(mapSurfaceListener);
//view.setSaveEnabled(true);
return view;
}
@Override
public void onPause() {
super.onPause();
mapHolder.onPause();
}
@Override
public void onResume() {
super.onResume();
mapHolder.onResume();
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i(TAG, "onDestroyView");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState executed");
}
}
MainActivity:
public class MainActivity extends AppCompatActivity implements SKPrepareMapTextureListener {
private final static String TAG = MainActivity.class.getSimpleName();
@Bind(R.id.viewpager)
ViewPager viewPager;
@Bind(R.id.tab_layout)
TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate executed");
if(savedInstanceState == null) {
prepareSKTextures();
}
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
hideSystemUI();
setupViewPager();
TabIcon.configureTabIcons(MainActivity.this, tabLayout);
super.onCreate(savedInstanceState);
}
private void hideSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
private void setupViewPager() {
viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(),
MainActivity.this));
tabLayout.setupWithViewPager(viewPager);
}
private void prepareSKTextures() {
final SKPrepareMapTextureThread prepThread = new SKPrepareMapTextureThread(this, App.skMapsResourcesPath, "SKMaps.zip", this);
prepThread.start();
}
@Override
public void onMapTexturesPrepared(boolean b) {
if(b) {
SKMapsInitSettings initMapSettings = new SKMapsInitSettings();
SKMapViewStyle skMapViewStyle = new SKMapViewStyle(App.skMapsResourcesPath + "daystyle/", "daystyle.json");
initMapSettings.setMapResourcesPaths(App.skMapsResourcesPath, skMapViewStyle);
SKMaps.getInstance().initializeSKMaps(this, initMapSettings);
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
//SKMaps.getInstance().destroySKMaps();
}
}
答案 0 :(得分:0)
我明白了。
刚刚在AndroidManifast中为我的活动添加了一行:
android:configChanges="orientation|screenSize"