MapFragment: bad performance after using the back button

我怕爱的太早我们不能终老 提交于 2019-12-30 00:07:06

问题


I have a performance issue when using MapFragment together with the action bar menu.

The bug emerges when three conditions are met

  1. Have a MapFragment instantiated.
  2. Trigger a fragment transaction from the options menu, replacing the map fragment with another fragment.
  3. Hit the back button and return to the map fragment. The performance is now noticeably degraded. Panning and zooming is very jerky.

Opening the options menu again and dismissing it again fixes the issue.

The behavior does not arise when

  • Triggering the fragment replacement from a view button instead from the options menu.
  • Triggering the fragment replacement right in onCreate()
  • replacing the blank fragment with MapFragment from the options menu
  • calling popBackStack from the options menu
  • using a ListFragment instead of a MapView

Minimal working example (requires access to Google Maps API):

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.maps.MapFragment;

public class MapFragmentBugActivity extends Activity {
    Fragment mMapFragment;
    String MAP = "Map";
    String BLANK = "Blank";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_bug);
        mMapFragment = new MapFragment();
        getFragmentManager().beginTransaction()
                .replace(R.id.main, mMapFragment)
                .commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(MAP);
        menu.add(BLANK);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Fragment fragment;

        if (item.getTitle().equals(MAP)) {
            fragment = mMapFragment;
        } else {
            fragment = new Fragment();
        }

        getFragmentManager()
                .beginTransaction()
                .replace(R.id.main, fragment)
                .addToBackStack(null)
                .commit();

        return true;
    }
}

Activity layout, nothing special

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true" />

回答1:


The fragment transaction is performed before the options menu is closed, this causes the weird behavior.

Instead of directly performing the fragment transaction, post it on the Handler. Once the options menu is closed, then the fragment transaction will be performed.

Try this :

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    final Fragment fragment;

    if (item.getTitle().equals(MAP)) {
        fragment = mMapFragment;
    } else { 
        fragment = new Fragment();
    } 

    Handler handler = new Handler();
    handler.post(new Runnable() {

        @Override
        public void run() {
            getFragmentManager()
            .beginTransaction()
            .replace(R.id.main, fragment)
            .addToBackStack(null)
            .commit();    
        }

    });     

    return true;
}


来源:https://stackoverflow.com/questions/25181860/mapfragment-bad-performance-after-using-the-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!