Pergunta

I'm working on go-back function with my fragments. I use the action bar as return button but the function onOptionsItemSelected does not work (the function may be even not called)

This code is in my FragmentActivity:

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == android.R.id.home) {          
                getActionBar().setHomeButtonEnabled(false);//disable the back button
                this.getFragmentManager().popBackStack();

                return true;
            }
            return super.onOptionsItemSelected(item);
        }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bottom_tab);

        FragmentTabHost tabHost = (FragmentTabHost)findViewById(R.id.bottom_tab_host);

        tabHost.setup(this, getSupportFragmentManager(), R.id.bottom_tab_content);

        //tabHost.addTab(tabHost.newTabSpec("explore").setIndicator("explore"), ListTabWidget.class, null);
        tabHost.addTab(tabHost.newTabSpec("browse").setIndicator("browse"), SearchList.class, null);
    }

And this code is in my Fragment(A):

            public class SearchList extends Fragment implements SearchView.OnQueryTextListener{

    FragmentActivity searchActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        searchActivity = (FragmentActivity) this.getActivity();

        View searchView = inflater.inflate(R.layout.search_list, container, false);

        SearchView searchBar = (SearchView)searchView.findViewById(R.id.browse_search);

        searchBar.setIconifiedByDefault(false); //Showing text field in search
        searchBar.setSubmitButtonEnabled(true);
        searchBar.setOnQueryTextListener(this);

        return searchView;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        /*Intent intent = new Intent(Intent.ACTION_SEARCH, null, searchActivity, BrowseGrid.class);
        intent.putExtra(SearchManager.QUERY, query);

        startActivity(intent);*/
        Bundle args = new Bundle();
        args.putString("search_query", query);

        BrowseGrid browseFragment = new BrowseGrid();
        browseFragment.setArguments(args);

        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.bottom_tab_content, browseFragment);
        transaction.addToBackStack(null);

        transaction.commit();

        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method stub
        return false;
    }


}

and Fragment(B):

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.grid_list, container, false);

        gridView = (GridView)view.findViewById(R.id.gridView);

        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

        return view;
    }

The aim is very simple. Pressing the back button, Fragment goes back from B to A. Am I missing something?

Foi útil?

Solução

As I found that the stack actually has nothing to pop(I wanna know why...), I choose an alternative to do so.

I don't know whether it is the best solution but it seems work.

replace

getFragmentManager().popBackStack();

to

FragmentManager fm = getFragmentManager();
            if (fm.getBackStackEntryCount()>0){
                //Log.d("Bottom Tab", "popping backstack");
                fm.popBackStack();
            } else {
                //Log.d("Bottom Tab", "nothing can pop");
                super.onBackPressed();
            }

Even the stack has nothing, it will override the onBackPressed() to go back.

Outras dicas

How about try using the SupportFragmentManager on both Activity and Fragment(A)

currently the Fragment (A) is using

FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();

While in you activity onOptionsItemSelected is using

this.getFragmentManager().popBackStack();

change it to:

getSupportFragmentManager().popBackStack();

Change this line , as you are handling the FragmentTransaction from Fragment class not Activity itself. You need to get getParentFragment() first.

FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();

To

FragmentTransaction transaction = getActivity().getParentFragment().getFragmentManager().beginTransaction();
        transaction.replace(R.id.bottom_tab_content, browseFragment);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top