简体   繁体   中英

clear backstack / history navigationdrawer item selected

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

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

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.

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.

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?

Use of android.support.v4.content.IntentCompat like this:

Intent intent = IntentCompat.makeRestartActivityTask(new ComponentName(DrawerActivity.this, Tracksfragment.class));
startActivity(intent);

You can set following flags in the intent while starting another activity:

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);

Hope this is what you were looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM