片段隐式地两次进入后栈

时间:2013-09-16 13:20:12

标签: android android-fragments

我有几个片段,如下所示:

MainActivity包含MainFragmentWebFragment(只是一个内部包含webview的片段):

 <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <fragment
            android:id="@+id/main_fragment"
            android:name="com.citylifeapps.cups.MainFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <fragment
            android:id="@+id/venue_fragment"
            android:name="com.citylifeapps.cups.WebFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </fragment>

 </FrameLayout>

MainFragment包含ListFragmentMapFragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map_fragment"
        android:name="com.citylifeapps.cups.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" />

    <fragment
        android:id="@id/list_fragment"
        android:name="com.citylifeapps.cups.ListFragment"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />

</RelativeLayout>

我的问题是以下情况:

  1. 启动应用,MainFragment从同一屏幕中的地图和列表开始
  2. 点击列表项,WebFragment打开
  3. 点击返回,MainFragment出现 - 正如预期的那样
  4. 点击另一个 - 再次显示WebFragment
  5. 最后一步(4)是问题所在。预期的行为是来自MainFragment(步骤3),单击返回将关闭应用程序,因为它是链中的最高点。

    我认为后面的堆栈出错了,

    我正在尝试调试它,但调试非常困难。

    这是FragmentManager日志:

    // Step 1
    
    09-16 16:00:15.683: D/FragmentManager(22278):     Op #0: HIDE WebFragment{42037ea8 #4 id=0x7f050082}
    09-16 16:00:15.683: D/FragmentManager(22278):     Op #1: SHOW MainFragment{41e678a0 #0 id=0x7f050081}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto ACTIVITY_CREATED: MainFragment{41e678a0 #0 id=0x7f050081}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto ACTIVITY_CREATED: MapFragment{41e68c60 #1 id=0x7f05002a}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto ACTIVITY_CREATED: SupportMapFragment{41e6adc8 #2 id=0x7f050084}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto ACTIVITY_CREATED: ListFragment{42058380 #3 id=0x7f05002b}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto ACTIVITY_CREATED: WebFragment{42037ea8 #4 id=0x7f050082}
    09-16 16:00:15.733: V/FragmentManager(22278): hide: WebFragment{42037ea8 #4 id=0x7f050082}
    09-16 16:00:15.733: V/FragmentManager(22278): show: MainFragment{41e678a0 #0 id=0x7f050081}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto STARTED: MainFragment{41e678a0 #0 id=0x7f050081}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto STARTED: MapFragment{41e68c60 #1 id=0x7f05002a}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto STARTED: SupportMapFragment{41e6adc8 #2 id=0x7f050084}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto STARTED: ListFragment{42058380 #3 id=0x7f05002b}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto STARTED: WebFragment{42037ea8 #4 id=0x7f050082}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto RESUMED: MainFragment{41e678a0 #0 id=0x7f050081}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto RESUMED: MapFragment{41e68c60 #1 id=0x7f05002a}
    09-16 16:00:15.733: V/FragmentManager(22278): moveto RESUMED: SupportMapFragment{41e6adc8 #2 id=0x7f050084}
    09-16 16:00:15.743: V/FragmentManager(22278): moveto RESUMED: ListFragment{42058380 #3 id=0x7f05002b}
    09-16 16:00:15.743: V/FragmentManager(22278): moveto RESUMED: WebFragment{42037ea8 #4 id=0x7f050082}
    
    // Step 2
    
    09-16 16:00:28.967: D/FragmentManager(22278):     Op #0: SHOW MainFragment{41e678a0 #0 id=0x7f050081}
    09-16 16:00:28.967: D/FragmentManager(22278):     Op #1: HIDE WebFragment{42037ea8 #4 id=0x7f050082}
    09-16 16:00:28.977: D/FragmentManager(22278):     Op #0: HIDE MainFragment{41e678a0 #0 id=0x7f050081}
    09-16 16:00:28.977: D/FragmentManager(22278):     Op #1: SHOW WebFragment{42037ea8 #4 id=0x7f050082}
    09-16 16:00:28.977: V/FragmentManager(22278): Bump nesting of MainFragment{41e678a0 #0 id=0x7f050081} to 1
    09-16 16:00:28.977: V/FragmentManager(22278): Bump nesting of WebFragment{42037ea8 #4 id=0x7f050082} to 1
    09-16 16:00:28.977: V/FragmentManager(22278): show: MainFragment{41e678a0 #0 id=0x7f050081}
    09-16 16:00:28.977: V/FragmentManager(22278): hide: WebFragment{42037ea8 #4 id=0x7f050082}
    09-16 16:00:28.977: V/FragmentManager(22278): Bump nesting of MainFragment{41e678a0 #0 id=0x7f050081} to 2
    09-16 16:00:28.977: V/FragmentManager(22278): Bump nesting of WebFragment{42037ea8 #4 id=0x7f050082} to 2
    09-16 16:00:28.977: V/FragmentManager(22278): hide: MainFragment{41e678a0 #0 id=0x7f050081}
    09-16 16:00:28.997: V/FragmentManager(22278): show: WebFragment{42037ea8 #4 id=0x7f050082}
    
    // Step 3
    
    09-16 16:00:39.238: D/FragmentManager(22278):     Op #0: HIDE MainFragment{41e678a0 #0 id=0x7f050081}
    09-16 16:00:39.238: D/FragmentManager(22278):     Op #1: SHOW WebFragment{42037ea8 #4 id=0x7f050082}
    09-16 16:00:39.238: V/FragmentManager(22278): Bump nesting of MainFragment{41e678a0 #0 id=0x7f050081} to 1
    09-16 16:00:39.238: V/FragmentManager(22278): Bump nesting of WebFragment{42037ea8 #4 id=0x7f050082} to 1
    09-16 16:00:39.238: V/FragmentManager(22278): hide: WebFragment{42037ea8 #4 id=0x7f050082}
    09-16 16:00:39.238: V/FragmentManager(22278): show: MainFragment{41e678a0 #0 id=0x7f050081}
    
    // Step 4
    
    09-16 16:00:53.724: D/FragmentManager(22278):     Op #0: SHOW MainFragment{41e678a0 #0 id=0x7f050081}
    09-16 16:00:53.724: D/FragmentManager(22278):     Op #1: HIDE WebFragment{42037ea8 #4 id=0x7f050082}
    09-16 16:00:53.724: V/FragmentManager(22278): Bump nesting of MainFragment{41e678a0 #0 id=0x7f050081} to 0
    09-16 16:00:53.724: V/FragmentManager(22278): Bump nesting of WebFragment{42037ea8 #4 id=0x7f050082} to 0
    09-16 16:00:53.724: V/FragmentManager(22278): show: WebFragment{42037ea8 #4 id=0x7f050082}
    09-16 16:00:53.724: V/FragmentManager(22278): hide: MainFragment{41e678a0 #0 id=0x7f050081}
    

    我不明白什么是错的,我唯一一次将WebFragment添加到后台堆栈是我单击列表项并在步骤2中打开它。另外我假设调用super.onBackPressed()弹回后面堆叠。

    以下是我的代码的简化版本:

    public class MainActivity extends ActionBarActivity {
    
        private boolean venueOrMapPageVisible = false;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main_activity);
    
            FragmentManager.enableDebugLogging(true);
            getSupportFragmentManager().beginTransaction()
            .hide(getVenueFragment())
            .show(getMainFragment())
            .commit();
    
        }
    
        @Override
        public void onBackPressed() {
    
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
            if (venueOrMapPageVisible) {
                setDrawerIndicatorEnabled(true);
                venueOrMapPageVisible = false;
            }
            super.onBackPressed();
    
        }
    
    
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (super.onOptionsItemSelected(item)) {
              return true;
            }
    
            switch (item.getItemId()) {
            case android.R.id.home:
                if (venueOrMapPageVisible) {
                    onBackPressed();
                }
                break;
    
    
            return false;
        }
    
    
    
        // called when a venue is clicked
        @Override
        public void onItemClick(JSONObject venue) {
    
            if (venue != null) {
                String vid = null;
                try {
                    vid = venue.getString("key");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                openVenue(vid);
            }
        }
    
    
        private void openVenue(String vid) {
            getVenueFragment().loadUrl(Consts.venueUri(vid));
    
            MainActivity.this
                    .getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
                    .hide(getMainFragment())
                    .show(getVenueFragment())
                    .addToBackStack(null)
                    .commit();
            venueOrMapPageVisible = true;
    
            // analytics open venue start time (set it only once)
            if (CupsTracker.venueStartTime == 0L) {
                CupsTracker.venueStartTime = System.currentTimeMillis();
            }
        }
    
    
        private WebFragment getVenueFragment() {
            return (WebFragment) getSupportFragmentManager().findFragmentById(R.id.venue_fragment);
        }
    
        private MainFragment getMainFragment() {
            return (MainFragment) getSupportFragmentManager().findFragmentById(R.id.main_fragment);
        }
    
    
    
    
        public void moveToMapFragment() {
            MapFragment mapFragment = (MapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
            ListFragment listFragment = (ListFragment) getSupportFragmentManager().findFragmentById(R.id.list_fragment);
            getSupportFragmentManager()
            .beginTransaction()
            .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
            .hide(listFragment)
            .addToBackStack(null)
            .commit();
            mapFragment.getView().setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            venueOrMapPageVisible = true;
    
        }
    
    }
    

    我怀疑由于MainActivity布局文件包含MainFragmentWebFragment,当它被夸大时,WebFragment会以某种方式隐式添加到后台堆栈或其他东西。有可能吗?

0 个答案:

没有答案