选项卡中包含片段的Android后退按钮

时间:2017-03-21 03:55:07

标签: android android-fragments android-tablayout

我正在使用标签在我的应用中的不同列表之间切换。当用户触摸列表中的项目时,以下是显示和隐藏详细信息的代码。我想知道如何添加一个返回到它所来自的正确列表的后退按钮。我正在更换碎片,所以我不知道标准后退按钮是否适用于我的情况?

    public class MainActivity extends AppCompatActivity implements 
        NavigationView.OnNavigationItemSelectedListener,
                    TabLayout.OnTabSelectedListener,
                    CustomerFragment.CustomerListListener,
                    CustomerDetailListener {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            ActionBar actionBar = getSupportActionBar();
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
        }

        public void onShowCustomerDetail(Customer customer){
                HostFragment hostFragment = (HostFragment) customPagerAdapter.getItem(viewPager.getCurrentItem());
                CustomerDetailFragment fragment = CustomerDetailFragment.newInstance(customer);
                hostFragment.replaceFragment(fragment, true);
        }

        public void onCloseCustomerDetail() {
            HostFragment hostFragment = (HostFragment) customPagerAdapter.getItem(viewPager.getCurrentItem());
            CustomerFragment fragment = new CustomerFragment();
            hostFragment.replaceFragment(fragment, true);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // just for testing. will do switch case here
            onBackPressed();
        }

        @Override
        public void onBackPressed() {
            if (getFragmentManager().getBackStackEntryCount() > 0) {
                getFragmentManager().popBackStack();
            } else {
                super.onBackPressed();
            }
        }
}

CustomPagerAdapter:

public class CustomPagerAdapter extends FragmentStatePagerAdapter {
    private final List<String> tabTitles = new ArrayList<String>() {{
        add("Messages");
        add("Customers");
        add("Jobs");
        add("Maps");
    }};

    private List<Fragment> tabs = new ArrayList<>();

    public CustomPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        initializeTabs();
    }

    private void initializeTabs() {
        tabs.add(HostFragment.newInstance(new MessageFragment()));
        tabs.add(HostFragment.newInstance(new CustomerFragment()));
        tabs.add(HostFragment.newInstance(new JobFragment()));
        tabs.add(HostFragment.newInstance(new MapFragment()));
    }

    @Override
    public Fragment getItem(int position) {
        return tabs.get(position);
    }

    @Override
    public int getCount() {
        return tabs.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles.get(position);
    }
}

HostFragment:

public class HostFragment extends BackStackFragment {
    private Fragment fragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_host, container, false);
        if (fragment != null) {
            replaceFragment(fragment, false);
        }
        return view;
    }

    public void replaceFragment(Fragment fragment, boolean addToBackstack) {
        if (addToBackstack) {
            getChildFragmentManager().beginTransaction().replace(R.id.hosted_fragment, fragment).addToBackStack(null).commit();
        } else {
            getChildFragmentManager().beginTransaction().replace(R.id.hosted_fragment, fragment).commit();
        }
    }

    public static HostFragment newInstance(Fragment fragment) {
        HostFragment hostFragment = new HostFragment();
        hostFragment.fragment = fragment;
        return hostFragment;
    }
}

4 个答案:

答案 0 :(得分:2)

由于我使用Child Fragment Manager在标签内设置我的片段的方式,我必须在我的onBackPressed中执行此操作才能使其工作:

@Override
public void onBackPressed() {
    HostFragment hostFragment = (HostFragment) customPagerAdapter.getItem(viewPager.getCurrentItem());
    FragmentManager fm = hostFragment.getChildFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        fm.popBackStack();
    } else {
        super.onBackPressed();
    }
}

答案 1 :(得分:0)

首先,你需要在使用它时在backstack中添加片段

请参阅下面的代码,用于创建片段并在其中添加到backstack

public final static String TAG_FRAGMENT = "HostFragment"; // used inside every fragment 

final HostFragment fragment = new HostFragment();
    final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
    transaction.addToBackStack(TAG_FRAGMENT);
    transaction.commit();

在内部活动onBackPressed()之后,你需要为你想要的每个背景片段弹出片段。

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack(); // pop fragment here
    } else {
        super.onBackPressed(); // after nothing is there default behavior of android works.
    }
}

答案 2 :(得分:0)

您需要做的就是在创建片段时将片段添加到后台堆栈。它看起来像这样:

CustomerDetailFragment fragment=CustomerDetailFragment.newInstance(customer);

getFragmentManager().beginTransaction()
                                .addToBackStack("Fragment Tag")
                                .replace(R.id.yourContainer, fragment, "Fragment Tag")
                                .commit();

完成此操作后,您只需使用主页后退按钮即可将片段弹出堆栈。如果你想使用自定义按钮返回,你必须实现自己的堆栈弹出,这将调用getFragmentManager().popBackStack();

答案 3 :(得分:0)

写一个功能onBackpress()

LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000