Using ActionBar Up Navigation in master/detail fragments

后端 未结 4 1343
生来不讨喜
生来不讨喜 2020-12-30 03:07

I have an app with master/detail layout (1 activity, 1 ListView fragment and 1 detail fragment). When the user clicks an item in the ListView, a fragment transaction instan

4条回答
  •  感情败类
    2020-12-30 03:47

    After much research and poking around, turns out that only reason why my activity was restarting during "up navigation" for master/detail configuration was because I left some code in the ListView Fragment's onOptionsItemSelected that was creating an intent to start the main activity in addition to my full fragment transaction code elsewhere. Below is the final implementation with which I got "up navigation" to work properly on both phone (multiple activities) and tablet (single activity/multi-pane) configurations. Thanks to Wolfram Rittmeyer for a couple of hints in his code (link in the comment section) that help me pinpoint my problem!

    Main Activity: Hosts the fragments and performs some other app-specific operations

    ListView Fragment: Handles "up navigation in table configuration

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if(mDualPane){
                    FragmentManager manager = getSherlockActivity().getSupportFragmentManager();
                    FragmentTransaction ft = manager.beginTransaction();
                    DetailFragment detailFragment = (DetailFragment)manager.findFragmentById(R.id.details);
                    ft.remove(detailFragment);
                    ft.commit();
                    manager.popBackStack();
                    getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                    getSherlockActivity().getSupportActionBar().setHomeButtonEnabled(false);
                }
                return true;
    
            // Other case statements...
    
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    

    Details Fragment: Handles up navigation in phone configuration

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    
        // Sets "up navigation" for both phone/tablet configurations
        ActionBar actionBar = getSherlockActivity().getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
            case android.R.id.home:
                if(!mDualPane){
                    Intent parentActivityIntent = new Intent(getSherlockActivity(), MainActivity.class);
                    parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(parentActivityIntent);
                    getSherlockActivity().finish();
                }
                return true;
    
            // Other case statements...
    
            default:
                return super.onOptionsItemSelected(item);
        }
    
    }
    

提交回复
热议问题