I have an android app with a view that contains 3 pages between which the user can navigate through swiping his finger (to the left or to the right - Horizontal View Paging). I downloaded “android.support.v4.view.ViewPager” and found the way to implement the swiping part mostly thanks to this tutorial here: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
My problem is that I want one of my views to launch a MapView. But to launch a MapView I need an activity that extends the MapActivity. If I have all my “RecordAdapter” class in a MapActivity that contains all the 3 views then I have a problem because each MapActivity can have only one MapView assigned to it. So if I swipe back to another page and then back to the map page the project crashes. The way I have it in the code is:
switch (position) {
case 0:
resId = R.layout.record1;
break;
case 1:
resId = R.layout.record2;
break;
case 2:
resId = R.layout.record3;
break;
}
Where “record3” contains my MapView. From experimenting crashing happens when I go back to “record1” and then to “record2” again. It seems that it launches a new MapView when it goes to record2 (?)! This must be the case because strangely enough if I have the MapView in the middle:
switch (position) {
case 0:
resId = R.layout.record1;
break;
case 1:
resId = R.layout.record3;
break;
case 2:
resId = R.layout.record2;
break;
}
it works ok! But this is not what I want (maybe I’ll need a 4rth page in the future too).
I hope the way I wrote it is not too complicated..
So what should I do? Any ideas?
EDIT: I found an answer to this. I needed to add "myPager.setOffscreenPageLimit(3);" when I call my ViewPager from my code. This way the views are kept alive!