0
Ich habe eine Aktivität mit einem TabLayout und einem ViewPager. Jeder Tab repräsentiert ein Fragment. Ich habe den SKMapViewHolder (skobbler) in meinem Fragment implementiert und es funktioniert gut. Aber jedes Mal, wenn ich meinen Bildschirm rotiere, wird das MapView neu erstellt. Wie kann ich es behalten?Retain Skobbler MapViewHolder
Hier den Code:
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>
Fragment:
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();
}
}