gpt4 book ai didi

android - 在 PopBackStack 上保留数据

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:26 27 4
gpt4 key购买 nike

我有一个使用两个 fragment 的 Activity ,MainFragmentDetailFragment。 MainFragment 有一个可扩展的 ListView ,其中公司名称作为组,位置名称作为子项。当用户从抽屉导航中选择一个类别时,它会更新适配器中的数据并更新可扩展列表,以显示正确的公司。

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

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

我的主要 fragment

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;
}
}

最佳答案

您应该在交易中使用add 而不是replace

关于android - 在 PopBackStack 上保留数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43647555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com