MapFragment: bad performance after using the back button

前端 未结 1 1626
我寻月下人不归
我寻月下人不归 2020-12-28 10:42

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
相关标签:
1条回答
  • 2020-12-28 11:29

    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;
    }
    
    0 讨论(0)
提交回复
热议问题