替换片段不会正确更改

时间:2014-10-29 22:57:20

标签: android android-fragments

当我尝试从第二个片段切换到第一个片段时,会出现问题。我正在使用ActionBar选项菜单图标来在不同的片段之间来回导航。

每个片段都有自己的菜单,该菜单与各个选项菜单项相关联。我似乎无法弄清楚为什么我能够从第一片段转换到第二片段,但我无法反过来。

我的代码中的任何内容都会突出,导致我无法导航回上一个片段吗?

我能够在没有任何问题的情况下在片段之间切换,但是在我将这些选项菜单项合并到导航后,它就停止了工作。

启动片段

public class SearchFragmentActivity extends Fragment implements OnClickListener {



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.activity_search_fragment,
            container, false);

    btnBasketball = (ImageButton) view.findViewById(R.id.btnBasketball);
    btnBasketball.setOnClickListener(this);
    setHasOptionsMenu(true);
    return view;

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    menu.clear();
    inflater.inflate(R.menu.navigation_search_event, menu);
    super.onCreateOptionsMenu(menu, inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case R.id.action_more:
        // do something with a dropdown
        break;

    }
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch (v.getId()) {

    case R.id.btnBasketball:

        changeFragment("Basketball");

        break;
    }

}

public void changeFragment(String sportName) {

    Bundle bundle = new Bundle();
    bundle.putString("SPORTNAME", sportName);

    Fragment fragment = new SearchDetailsFragmentActivity();
    fragment.setArguments(bundle);

    FragmentManager fm = getFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();

    transaction.setCustomAnimations(R.anim.cell_left_in,
            R.anim.cell_right_out);
    transaction.replace(R.id.searchFragment, fragment);

    transaction.commit();
}

}

第二片段

public class SearchDetailsFragmentActivity extends Fragment {

TextView tvSportsName;
GridView gView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.activity_search_details_fragment,
            container, false);

    String sportsName = getArguments().getString("SPORTNAME");

    tvSportsName = (TextView) view.findViewById(R.id.tvSportsName);
    tvSportsName.setText(sportsName);

    // SportAdapter sAdapter = new SportAdapter(getActivity(), lstSports);

    gView = (GridView) view.findViewById(R.id.gridViewSearch);

    setHasOptionsMenu(true);
    return view;

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    menu.clear();
    inflater.inflate(R.menu.navigation_search_details_event, menu);
    super.onCreateOptionsMenu(menu, inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case R.id.action_cancel:

        changeFragment();
        break;

    }
    return true;
}

public void changeFragment() {

    Fragment fragment = new SearchFragmentActivity();

    FragmentManager fm = getFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();

    transaction.setCustomAnimations(R.anim.cell_left_in,
            R.anim.cell_right_out);
    transaction.replace(R.id.searchDetailsFragment, fragment);

    transaction.commit();
}

}

与Second Fragment相关联的选项菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >

    <item
        android:id="@+id/action_cancel"
        android:icon="@drawable/ic_content_remove"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="always"/>

</menu>

更新

这些更改允许我从SearchDetailsFragmentActivity&gt;&gt;向后导航使用SoftKey后退按钮进行SearchFragmentActivity。

SearchDetailsFragmentActivity中changeFragment()方法的更改

    public void changeFragment() {

    Fragment fragment = new SearchFragmentActivity();

    FragmentManager fm = getFragmentManager();
    //FragmentTransaction transaction = fm.beginTransaction();

    //transaction.setCustomAnimations(R.anim.cell_left_in,
            //R.anim.cell_right_out);
    //transaction.replace(R.id.searchDetailsFragment, fragment);

    //transaction.commit();
    fm.popBackStack();
}
SearchFragmentActivity中的

public void changeFragment(String sportName) {

    Bundle bundle = new Bundle();
    bundle.putString("SPORTNAME", sportName);

    Fragment fragment = new SearchDetailsFragmentActivity();
    fragment.setArguments(bundle);

    FragmentManager fm = getFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();

    transaction.setCustomAnimations(R.anim.cell_left_in,
            R.anim.cell_right_out);
    transaction.replace(R.id.searchFragment, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

更新2

我在OnOptionItemSelected()中放置一个Log.e来查看按钮是否正常触发,看起来好像不是。没有任何东西被记录下来。

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case R.id.action_cancel:
        Log.e("IS THIS WORKING", "THIS IS WORKING");   <<< NOT FIRING
        changeFragment();
        break;

    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

更换片段时,您需要指明该事务是否可逆(即它是否进入后台)。对于第一个片段事务,您通常不想这样做(不想弹出到空视图)。对于片段替换,您希望使用可选标记(稍后可以与addToBackStack()一起使用)指定popBackStack(String)

例如,在这些行之间:

transaction.replace(R.id.searchDetailsFragment, fragment);

transaction.commit();

添加:

transaction.replace(R.id.searchDetailsFragment, fragment);
transaction.addToBackStack(null);
transaction.commit();