MapFragment:使用后退按钮后,糟糕的表现 [英] MapFragment: bad performance after using the back button

查看:156
本文介绍了MapFragment:使用后退按钮后,糟糕的表现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 MapFragment 的操作栏菜单时使用起来性能问题。

这个错误出现在满足三个条件

  1. 有一个 MapFragment 实例化。
  2. 触发从选项菜单中的片段的交易,与另一个片段取代地图片段。
  3. 点击返回按钮,返回到地图片段。性能现在明显降低。平移和缩放非常生涩。

再次打开选项菜单,然后再次驳回其修复该问题。

该行为不会出现在

  • 从选项菜单触发片段置换从视图按钮来代替。
  • 在触发权的onCreate片段置换()
  • 从选项菜单,MapFragment更换空白片段
  • 调用 popBackStack 从选项菜单
  • 在使用,而不是一个图形页面
  • 系统ListFragment

最小工作示例(需要访问谷歌地图API):

 进口android.app.Activity;
进口android.app.Fragment;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;

进口com.google.android.gms.maps.MapFragment;

公共类MapFragmentBugActivity延伸活动{
    片段mMapFragment;
    字符串映射=地图;
    字符串空白=空白;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_fragment_bug);
        mMapFragment =新MapFragment();
        getFragmentManager()的BeginTransaction()
                .replace(R.id.main,mMapFragment)
                。承诺();
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        menu.add(MAP);
        menu.add(空白);
        返回super.onCreateOptionsMenu(菜单);
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        片段片段

        如果(item.getTitle()。等于(MAP)){
            片段= mMapFragment;
        } 其他 {
            片段=新片段();
        }

        getFragmentManager()
                .beginTransaction()
                .replace(R.id.main,片段)
                .addToBackStack(空)
                。承诺();

        返回true;
    }
}
 

活动的布局,没有什么特别的。

 < XML版本=1.0编码=UTF-8&GT?;

<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /主
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:可点击=真/>
 

解决方案

该片段的交易进行前的选项菜单关闭,这将导致怪异的行为。

而不是直接执行片段交易的

,其发布的处理程序。一旦选项菜单被关闭,则该片段事务​​将被执行。

试试这个:

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    最后片段的片段;

    如果(item.getTitle()。等于(MAP)){
        片段= mMapFragment;
    } 其他 {
        片段=新片段();
    }

    处理程序处理程序=新的处理程序();
    handler.post(新的Runnable(){

        @覆盖
        公共无效的run(){
            getFragmentManager()
            .beginTransaction()
            .replace(R.id.main,片段)
            .addToBackStack(空)
            。承诺();
        }

    });

    返回true;
}
 

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" />

解决方案

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;
}

这篇关于MapFragment:使用后退按钮后,糟糕的表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆