明确backstack /历史navigationdrawer项目选择 [英] clear backstack / history navigationdrawer item selected
问题描述
在MainActivity我有一个看起来像这样的NavigationDrawer:
In the MainActivity I have the NavigationDrawer which looks like this:
private void displayView(int position) {
ListFragment listFragment = null;
switch (position) {
case 0:
listFragment = new AlbumFragment();
break;
case 1:
listFragment = new TrackFragment();
break;
default:
break;
}
if (listFragment != null) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frame_container, listFragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
在我的AlbumFragment我可以通过点击listItems中切换到TrackFragment。
Within my AlbumFragment I can switch to the TrackFragment by clicking on listitems.
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// get album & artist of selected album
String album = ((TextView) v.findViewById(R.id.albumTitle)).getText().toString();
String artist = ((TextView) v.findViewById(R.id.albumArtist)).getText().toString();
String[] albumFilter = {"albumFilter", album, artist};
// Create new fragment and transaction
Fragment newFragment = new TrackFragment();
Bundle args = new Bundle();
args.putStringArray("filter", albumFilter);
newFragment.setArguments(args);
FragmentTransaction transaction = this.getActivity().getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.frame_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
现在发生以下问题:如果我preSS的返回按钮,一切都很好,我回到了AlbumFragment。但如果我打开NavigationDrawer并选择Tracksfragment然后preSS的后退按钮我回到了AlbumFragment和它背后仍然是我的TrackFragment。
Now the following problem occurs: If I press the Backbutton everything is fine and I'm back in the AlbumFragment. But If I open the NavigationDrawer and select the Tracksfragment and then press the Backbutton I get back to the AlbumFragment and behind it is still my TrackFragment.
进一步解释:当我选择在AlbumFragment一个相册只能从这张专辑的曲目显示。如果我从NavDrawer选择跟踪所有的曲目都显示出来。
Further Explanation: When I select an Album in the AlbumFragment only the Tracks from this album are shown. If I select Tracks from the NavDrawer ALL tracks are displayed.
所以基本上我要的是,整个Backstack,历史是只要我选择从NavDrawer一个项目清理。
我已经尝试过的大部分解决类似问题的发现在这里,但没有unfortuneatley为我工作至今。
So basically all I want is, that the entire Backstack-History is cleaned as soon as I select an Item from the NavDrawer. I already tried most of the solutions to similar problems found on here but unfortuneatley nothing worked for me so far.
有没有人得到了一个解决这个问题?
Has anybody got a solution for this problem?
推荐答案
使用android.support.v4.content.IntentCompat的是这样的:
Use of android.support.v4.content.IntentCompat like this:
Intent intent = IntentCompat.makeRestartActivityTask(new ComponentName(DrawerActivity.this, Tracksfragment.class));
startActivity(intent);
这篇关于明确backstack /历史navigationdrawer项目选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!