Android的图形页面与片段不能添加两次? [英] Android mapview with fragments can't be added twice?
问题描述
我现在用的是android的兼容类黑客使用mapviews在这里找到一个片段:<一href="https://github.com/petedoyle/android-support-v4-googlemaps">https://github.com/petedoyle/android-support-v4-googlemaps
I am using the android compatibility class with the hack for using mapviews in a fragment found here: https://github.com/petedoyle/android-support-v4-googlemaps
不幸的是,我发现是,如果mapfragment被从活动中删除,然后重新添加,我得到了你只能有一个MapActivity一个图形页面的错误。
Unfortunately, what I am finding is that if the mapfragment gets removed from the activity, and then readded, I get the "You are only allowed to have a single MapView in a MapActivity" error."
我理解错误背后的原理,并试图摧毁的碎片的onPause方法的MapView。不幸的是我似乎无法彻底摧毁的MapView,因为我仍然得到它。我的code是这样的:
I understand the principle behind the error, and tried destroying the mapview in the fragments onPause method. Unfortunately I can't seem to destroy the mapview completely, since I am still getting it. My Code looks like this:
private RelativeLayout layout;
private MapView mp;
public void onResume(){
super.onResume();
Bundle args = getArguments();
if(mp == null)
{
mp = new MapView(getActivity(), this.getString(R.string.map_api_key));
mp.setClickable(true);
}
String request = args.getString("requestId");
layout = (RelativeLayout) getView().findViewById(R.id.mapholder);
layout.addView(mp);
//TextView txt = (TextView) getView().findViewById(R.id.arguments);
//txt.setText(request);
}
public void onPause(){
super.onPause();
layout.removeView(mp);
mp = null;
}
有没有人有任何想法什么的参考,我忽略了这里毁灭?
Does anyone have any thoughts on what the reference I am neglecting to destroy here?
推荐答案
我遇到了同样的问题。这是我如何解决它:
I encountered the same issue. Here is how I solved it :
-
因为它应该是在活动的MapView只有一个实例,我在活动onCreate方法初始化:
As it should be only one instance of the mapView in the activity, I initialize it in onCreate method in Activity :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// initialize MapView programmatically to be used in Fragment :
this.mActivityMapView = new MapView(MainActivity.this, getString(R.string.debug_mapview_apikey));
setContentView(R.layout.activity_main);
}
然后,我恢复它在片段onCreateView方式:
Then I recover it in the fragment onCreateView method :
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.mMapView = ((MainActivity) getActivity()).getMapView();
return this.mMapView;
}
和我毁掉它在片段的onDestroy方法:
And I destroy it in the fragment onDestroy method :
public void onDestroy() {
NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout) this.mMapView.getParent();
parentView.removeView(this.mMapView);
super.onDestroy();
}
这篇关于Android的图形页面与片段不能添加两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!