我有一个Activity
MapFragment
,我使用Activity
以编程方式添加到FragmentTransaction
:
private static final String MAP_FRAGMENT_TAG = "map";
private MapFragment mapFragment = null;
...
protected void onCreate(Bundle savedInstanceState) {
...
mapFragment = (MapFragment) getFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);
if (mapFragment == null) {
mapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_wrapper, mapFragment, MAP_FRAGMENT_TAG);
fragmentTransaction.commit();
}
...
}
标准方式。然后我从GoogleMap
获取mapFragment
实例并设置其设置,设置监听器,用它做事。一切正常。
然后当用户完成地图后,会触发AsyncTask
以显示ProgressDialog
,执行某些操作,将不同的片段放入fragment_wrapper
并关闭{{ 1}}再次:
ProgressDialog
一切仍然正常。用户在private class GetFlightsTask extends AsyncTask<Double, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// the activity context has been passed to the AsyncTask through its constructor
loadingFlightsSpinner = new ProgressDialog(context);
// setting the dialog up
loadingFlightsSpinner.show();
}
@Override
protected String doInBackground(Double... params) {
// some pretty long remote API call
// (loading a JSON file from http://some.website.com/...)
}
@Override
protected void onPostExecute(String flightsJSON) {
super.onPostExecute(flightsJSON);
// here I do stuff with the JSON and then I swtich the fragments like this
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
FlightsFragment fragment = new FlightsFragment();
fragmentTransaction.replace(R.id.fragment_wrapper, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
loadingFlightsSpinner.dismiss();
}
中执行某些操作,然后可能决定返回到地图。按后退按钮,地图再次弹出。这就是地图变得迟钝的时候。它上面的国家/城市名称加载速度非常慢,它在移动地图方面严重滞后......而且我不知道为什么,我在弹出FlightsFragment
时没有做任何事情。
有趣的是,例如在按下 home 按钮然后再次返回应用程序时,它会得到修复...
我做错了什么?
感谢您的任何想法。
答案 0 :(得分:2)
只有按下后退按钮才会延迟?
如果那个问题试图阻止后退按钮或让它退出应用程序尝试此代码:
@Override
public void onBackPressed(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You wanna leave the aplication?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
或尝试使用此代码将一个地图片段放入另一个片段(嵌套的Map Fragment)中,这个片段在几个星期前就适用于我:
Java类:
public class Yourfragment extends Fragment {
private MapView mMapView;
private GoogleMap mMap;
private Bundle mBundle;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
// TODO handle this situation
}
mMapView = (MapView) inflatedView.findViewById(R.id.map);
mMapView.onCreate(mBundle);
setUpMapIfNeeded(inflatedView);
return inflatedView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBundle = savedInstanceState;
}
private void setUpMapIfNeeded(View inflatedView) {
if (mMap == null) {
mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
mMapView.onDestroy();
super.onDestroy();
}
}
XML:
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
将此代码放在post execute:
上View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
// TODO handle this situation
}
mMapView = (MapView) inflatedView.findViewById(R.id.map);
mMapView.onCreate(mBundle);
setUpMapIfNeeded(inflatedView);
return inflatedView;
并在oncreateview上调用assynctask
试试这个:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Call assyncTask
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBundle = savedInstanceState;
}
private void setUpMapIfNeeded(View inflatedView) {
if (mMap == null) {
mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
mMapView.onDestroy();
super.onDestroy();
}
private class GetFlightsTask extends AsyncTask<Double, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// if I remove the next line, everything gets fixed
loadingFlightsSpinner.show();
}
@Override
protected String doInBackground(Double... params) {
// some pretty long remote API call
// (loading a JSON file from http://some.website.com/flights?...)
// works fine
String flightsJSON = loadJSON("flights?flyFrom=CZ&to=...");
}
@Override
protected void onPostExecute(String flightsJSON) {
super.onPostExecute(flightsJSON);
loadingFlightsSpinner.dismiss();
// here I do stuff with the JSON and then replace the fragment
dohardwork()
}
public view dohardwork(){
View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
// TODO handle this situation
}
mMapView = (MapView) inflatedView.findViewById(R.id.map);
mMapView.onCreate(mBundle);
setUpMapIfNeeded(inflatedView);
return inflatedView;
}
答案 1 :(得分:1)
我做了一个简单的测试:
public class MapFragmentOnBackStackExample extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_fragment_on_back_stack_example);
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentById(R.id.fragment_container);
if (f == null) {
f = SupportMapFragment.newInstance();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fragment_container, f);
transaction.commit();
}
}
public void onAddFragmentClick(View view) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragment_container, new MyFragment());
transaction.addToBackStack(null);
transaction.commit();
}
public static class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText("MyFragment: " + hashCode());
return textView;
}
}
}
并且看不到任何问题。
我在评论if (f == null) {
时可以看到问题,让它始终在旋转时创建新的片段,这显然是错误的,但这带来了一些怀疑。
你能同时在内存中看到多个MapFragment吗?尝试使用Eclipse Memory Analyzer(MAT)。
答案 2 :(得分:1)
我已经通过在ProgressDialog
的{{1}}方法的末尾而不是在AsyncTask
方法的开头解雇doInBackground()
来修复它。
这有点奇怪,因为我实际上认为我不应该在onPostExecute()
方法中触摸UI中的内容......如果有人想详细说明一下,我会很高兴知道为什么它就像这样。