保留PopBackStack上的数据

时间:2017-04-27 02:15:05

标签: android android-fragments expandablelistview android-navigation-drawer

我有一个活动使用两个片段,MainFragmentDetailFragment。 MainFragment具有可扩展的列表视图,其公司名称为组,位置名称为子项。当用户从导航栏中选择一个类别时,它会更新适配器中的数据并更新可扩展列表,显示正确的公司。

当用户选择一个位置时,它会打开DetailFragment,其中包含有关该位置和员工的信息。我遇到的问题是当我退后一步时。当我点击后退按钮时,它会重置为默认类别,并且我已经发现它是因为它再次调用onViewCreated()方法,这是我设置我的方法视图和适配器。奇怪的是,它通过索引记住扩展了哪些列表项。因此,如果我在另一个类别中展开了第三个项目,则默认类别中的第三个项目仍然会展开。

我想知道的是,我怎样才能保持按下时的设置?

My MainFragment

public class MainFragment extends Fragment implements ExpandableListView.OnChildClickListener, NavigationSelectedListener {

    private CompaniesExpandableListAdapter adapter;

    public MainFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        connectViews(view);
    }

    private void connectViews(View view) {
        final ExpandableListView companyListView = (ExpandableListView) view.findViewById(R.id.companyListView);

        final Navigation navigationItem = ((MainActivity) getActivity()).getNavigationItems().first();
        getActivity().setTitle(navigationItem.getTitle());

        adapter = new CompaniesExpandableListAdapter(navigationItem.getCompanies());
        companyListView.setAdapter(adapter);
        companyListView.setOnChildClickListener(this);
    }

    // ExpandableListView.OnChildClickListener
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        final DetailFragment detailFragment = new DetailFragment();

        final Bundle args = new Bundle();
        args.putInt(getString(R.string.extra_location_key), ((Locations) adapter.getChild(groupPosition, childPosition)).getPrimaryKey());

        detailFragment.setArguments(args);

        getFragmentManager()
                .beginTransaction()
                .replace(R.id.contentFrame, detailFragment, getString(R.string.tag_fragment_detail))
                .addToBackStack(null)
                .commit();

        return false;
    }

    // NavigationSelectedListener
    @Override
    public void onNavigationItemSelected(Navigation navigationItem) {
        // Go back to the main fragment.
        getFragmentManager().popBackStack(getString(R.string.tag_fragment_main), FragmentManager.POP_BACK_STACK_INCLUSIVE);

        // Send the selected item to the adapter.
        adapter.updateData(navigationItem.getCompanies());
        getActivity().setTitle(navigationItem.getTitle());
    }
}

我的MainActivity

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private NavigationSelectedListener navigationCallback;

    private DrawerLayout drawerLayout;

    private RealmResults<Navigation> navigationItems;

    private MainFragment mainFragment = new MainFragment();

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

        buildNavigationDrawer();
        connectViews();
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) drawerLayout.closeDrawer(GravityCompat.START);
        else super.onBackPressed();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        navigationCallback.onNavigationItemSelected(navigationItems.get(item.getItemId()));

        drawerLayout.closeDrawer(GravityCompat.START);

        return true;
    }

    private void connectViews() {
        navigationCallback = mainFragment;

        getFragmentManager()
                .beginTransaction()
                .replace(R.id.contentFrame, mainFragment, getString(R.string.tag_fragment_main))
                .commit();
    }

    private void buildNavigationDrawer() {
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

            @Override
            public void onDrawerStateChanged(int newState) {
                super.onDrawerStateChanged(newState);

                // Hides the device keyboard when the navigation drawer is opened.
                if (getCurrentFocus() != null) ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            }
        };

        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        fillNavigationDrawer();
    }

    public RealmResults<Navigation> getNavigationItems() {
        return navigationItems;
    }
}

1 个答案:

答案 0 :(得分:2)

您应该在交易中使用add代替replace