首页 文章

Android - 使用popBackStack()不会从堆栈中删除片段

提问于
浏览
0

我正在制作一个这样的简单应用:

enter image description here

“Push Fragment”按钮用新的片段替换当前片段 . “Pop Fragment”按钮应该从堆栈中删除一个片段 .

这是活动的代码:

public class ActivityWithMenu extends Activity{
    private WVFragment wvFragment;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_with_menu);

        wvFragment = (WVFragment) getFragmentManager().findFragmentById(R.id.wv_fragment);

        Button btnBack = (Button)findViewById(R.id.btnPopFragment);
        btnBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getFragmentManager();
                if (fm.getBackStackEntryCount() > 0) {
                    Log.i("FRAGMENT_EXAMPLE", "will pop the current fragment");
                    fm.popBackStack();
                }
            }
        });

        Button btnPush = (Button)findViewById(R.id.btnPushFragment);
        btnPush.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.wv_fragment, wvFragment);
                transaction.addToBackStack(null);
                transaction.commit();

                WebView wv = (WebView)wvFragment.getView().findViewById(R.id.webview);
                wv.setWebViewClient(new WebViewClient());
                wv.loadUrl("http://www.google.com");
            }
        });
    }
}

这是activity_with_menu视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Pop Fragment"
      android:id="@+id/btnPopFragment" 
      android:layout_alignParentTop="true"
     ></Button>
   <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Push Fragment"
      android:id="@+id/btnPushFragment" 
      android:layout_below="@+id/btnPopFragment"
     ></Button>
   <fragment 
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_below="@+id/btnPushFragment"
       android:id="@+id/wv_fragment"
       class="com.lucasdev.baseactivitydemo.WVFragment"
       />
</RelativeLayout>

这是碎片:

public class WVFragment extends Fragment{
    private View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.wv_fragment, container, false);

        WebView wv = (WebView)view.findViewById(R.id.webview);
        wv.loadUrl("http://www.stackoverflow.com");

        return view;
    }
}

这是片段视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>

我不知道我做错了什么,因为“Pop Fragment”在推出新片段后没有删除任何片段 .

但我知道popBackStack()被调用,因为消息总是在控制台中打印 .

那么,我做错了什么?

3 回答

  • 1

    每次单击Push时都必须添加新的 Fragment .

    fragment = new WVFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.wv_fragment, fragment );
    transaction.addToBackStack(null);
    transaction.commit();
    
    WebView wv = (WebView)fragment.getView().findViewById(R.id.webview);
    

    因为如果添加片段的相同(实例),则没有其他片段 .

  • 1

    而不是使用 popBackStack() 来弹出当前片段,只需使用活动的本机BackButton方法 .

    example:

    Button btnBack = (Button)findViewById(R.id.btnPopFragment);
        btnBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
    
                ActivityWithMenu.this.onBackPressed();
    
            }
        });
    

    onBackPressed 将具有弹出片段所需的相同功能 .

  • 1

    首先,尝试从XML中删除您的片段并在那里保留一个空容器:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
       <Button
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Pop Fragment"
          android:id="@+id/btnPopFragment" 
          android:layout_alignParentTop="true"
         ></Button>
       <Button
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Push Fragment"
          android:id="@+id/btnPushFragment" 
          android:layout_below="@+id/btnPopFragment"
         ></Button>
       <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" 
        android:id="@+id/fragmentcontainer">
    </FrameLayout>
    </RelativeLayout>
    

    然后你需要从代码中添加你的Fragment ie com.lucasdev.baseactivitydemo.WVFragment片段,即你的btnPush的 onClick() .

    让我知道,如果仍然存在问题 .

相关问题