Sound star Android Navigation Drawer Item select

SoundCloud clone for Android and Web
anonymus
Posts: 67
Joined: Mon Aug 24, 2015 1:29 pm

Sound star Android Navigation Drawer Item select

Postby anonymus » Thu Aug 27, 2015 8:36 am

Hi,

Whenever i open up the soundstar it will open up the StreamFragment by default, if i select different item from navigation drawer it open up the corresponding fragment after that if i press back button it will close the app it wont go to previous fragment nor default StreamFragment . How can i achieve this??

sagar
Posts: 37
Joined: Mon Aug 24, 2015 11:23 am

Re: Sound star Android Navigation Drawer Item select

Postby sagar » Thu Aug 27, 2015 8:48 am

Hi,

it will be easy to trace if you post the code

anonymus
Posts: 67
Joined: Mon Aug 24, 2015 1:29 pm

Re: Sound star Android Navigation Drawer Item select

Postby anonymus » Thu Aug 27, 2015 8:55 am

Code: Select all

if (savedInstanceState == null) {
         // on first time display view for first nav item
         displayView(1);
      }
      
here in the above code to call the default  1st fragment
and below is the code to replace the fragment according to navigation drawer item select

Fragment fragment = null;
   private void displayView(int position/* , ImageView iv_icon */) {
      // update the main content by replacing fragments
      
      switch (position) {
      case 0:
         fragment = new ProfileFragment();
         // Toast.makeText(getApplicationContext(), "Main",
         // Toast.LENGTH_LONG).show();
         break;
      case 1:
         fragment = new StreamFragment();
         // iv_icon.setImageResource(R.drawable.drawer_stream_active);
         break;
      case 2:
         fragment = new ExploreFragment();
         // iv_icon.setImageResource(R.drawable.drawer_explore_active);
         break;
      case 3:
         fragment = new LikesFragment();
         // iv_icon.setImageResource(R.drawable.drawer_likes_active);
         break;
      case 4:


      if (fragment != null) {
         FragmentManager fragmentManager = getSupportFragmentManager();
         fragmentManager.beginTransaction()
               .replace(R.id.frame_container, fragment).commit();
}

sagar
Posts: 37
Joined: Mon Aug 24, 2015 11:23 am

Re: Sound star Android Navigation Drawer Item select

Postby sagar » Thu Aug 27, 2015 9:07 am

Hi,

To achieve this after replacing the previous fragment with the current fragment you need to add the previous fragment to the back stack and override
onBackPressed() in your activity and check if there any fragments in the back stack, if there pop the same so it will inflate previous fragment else call the super.onBackPressed() so it will close your app.

To add the fragment to backstack follow the below code.

Code: Select all

FragmentManager fragmentManager = getSupportFragmentManager();
         fragmentManager.beginTransaction()
               .replace(R.id.frame_container, fragment).addToBackStack(null)commit().commit();

and override onBackPressed() like below

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

anonymus
Posts: 67
Joined: Mon Aug 24, 2015 1:29 pm

Re: Sound star Android Navigation Drawer Item select

Postby anonymus » Mon Aug 31, 2015 12:57 pm

Good Explanation..
Thank you..


Return to “SoundStar”

Who is online

Users browsing this forum: No registered users and 0 guests