3

I have an activity that has 3 tab buttons and each of them will bring their own fragment into screen. And one of the fragmnets has a musicplayer and imagebuttons that changes their icons when i click them. ( like play button changes drawable to pause )

The problem is, when I click this play imagebutton and change tabs and return the tab with musicplayer, the imagebutton resets. I think fragmentmanager.replace method creates a new instance of fragment. How can i avoid this.

Here is my code snippet.

public class HomeActivity extends SlidingFragmentActivity {

private LocationTracker lt;
private ImageButton imgbTimeline, imgbProfile, imgbMusic;
private Fragment menufragment = new Plik_MenuFragment();
private Fragment timeline = new Plik_TimelineFragment();
private Fragment profile = new Plik_ProfileFragment();
private Fragment musicPlayer = new Plik_MusicPlayerFragment();
private ViewPager viewpager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    // inner slidemenu setting class executing.
    setInitials();

    // Fragment işleri



    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(R.id.slidemenu_container, menufragment);
    ft.add(R.id.homepage_container, profile);
    ft.add(R.id.homepage_container, musicPlayer);
    ft.add(R.id.homepage_container, timeline);
    ft.commit();

    OnClickListener ocl = new OnClickListener() {

        @Override
        public void onClick(View v) {

            FragmentTransaction ft = getFragmentManager()
                    .beginTransaction();
            // TODO Auto-generated method stub

            switch(v.getId()){

            case R.id.imgbTimeline :
                ft.replace(R.id.homepage_container, timeline);
                ft.addToBackStack(null);
                imgbTimeline.setBackgroundColor(Color.BLACK);
                imgbProfile.setBackgroundColor(Color.GRAY);
                imgbMusic.setBackgroundColor(Color.GRAY);
                break;

            case R.id.imgbProfile :
                ft.replace(R.id.homepage_container, profile);
                ft.addToBackStack(null);
                imgbTimeline.setBackgroundColor(Color.GRAY);
                imgbProfile.setBackgroundColor(Color.BLACK);
                imgbMusic.setBackgroundColor(Color.GRAY);
                break;

            case R.id.imgbMusic :
                ft.replace(R.id.homepage_container, musicPlayer);
                ft.addToBackStack(null);
                imgbTimeline.setBackgroundColor(Color.GRAY);
                imgbProfile.setBackgroundColor(Color.GRAY);
                imgbMusic.setBackgroundColor(Color.BLACK);
                break;
                default:
                    break;

            }

            ft.commit();

        }
    };

    imgbTimeline.setOnClickListener(ocl);
    imgbProfile.setOnClickListener(ocl);
    imgbMusic.setOnClickListener(ocl);

3 Answers3

1

I think the problem is you are replacing all time fragments that are in the manager. Try controlling the visible fragment, and use this code:

public class HomeActivity extends SlidingFragmentActivity {

private LocationTracker lt;
private ImageButton imgbTimeline, imgbProfile, imgbMusic;
private Fragment menufragment = new Plik_MenuFragment();
private Fragment timeline = new Plik_TimelineFragment();
private Fragment profile = new Plik_ProfileFragment();
private Fragment musicPlayer = new Plik_MusicPlayerFragment();
private ViewPager viewpager;
private Fragment visible;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    // inner slidemenu setting class executing.
    setInitials();

    // Fragment işleri
    // SUPOSE timeline is main fragment:
    visible = timeline;


    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(R.id.slidemenu_container, menufragment);
    ft.add(R.id.homepage_container, profile);
    ft.add(R.id.homepage_container, musicPlayer);
    ft.add(R.id.homepage_container, timeline);
    ft.commit();

    OnClickListener ocl = new OnClickListener() {

        @Override
        public void onClick(View v) {

            FragmentTransaction ft = getFragmentManager()
                    .beginTransaction();
            // TODO Auto-generated method stub

            switch(v.getId()){

            case R.id.imgbTimeline :
                ft.hide(visible);
                ft.show(timeline);
                ft.addToBackStack(null);
                visible = timeline;
                imgbTimeline.setBackgroundColor(Color.BLACK);
                imgbProfile.setBackgroundColor(Color.GRAY);
                imgbMusic.setBackgroundColor(Color.GRAY);

                break;

            case R.id.imgbProfile :
                ft.hide(visible);
                ft.show(profile);
                ft.addToBackStack(null);
                visible = profile;
                ft.addToBackStack(null);
                imgbTimeline.setBackgroundColor(Color.GRAY);
                imgbProfile.setBackgroundColor(Color.BLACK);
                imgbMusic.setBackgroundColor(Color.GRAY);
                break;

            case R.id.imgbMusic :
                ft.hide(visible);
                ft.show(musicPlayer);
                ft.addToBackStack(null);
                visible = musicPlayer;
                imgbTimeline.setBackgroundColor(Color.GRAY);
                imgbProfile.setBackgroundColor(Color.GRAY);
                imgbMusic.setBackgroundColor(Color.BLACK);
                break;
                default:
                    break;

            }

            ft.commit();

        }
    };

    imgbTimeline.setOnClickListener(ocl);
    imgbProfile.setOnClickListener(ocl);
    imgbMusic.setOnClickListener(ocl);
Neonamu
  • 736
  • 1
  • 7
  • 21
1

From Android Developers:

When you remove or replace a fragment and add the transaction to the back stack, the fragment that is removed is stopped (not destroyed). If the user navigates back to restore the fragment, it restarts. If you do not add the transaction to the back stack, then the fragment is destroyed when removed or replaced.

So if you're calling ft.addToBackStack() it should not be destroying your Fragments.

Log your Fragments lifecycle methods to understand better what's happening.

@Override
protected void onResume() {
    super.onResume();
    Log.d("onResume", "Fragment X");
}

And do the same for onPause,

Teo Inke
  • 5,928
  • 4
  • 38
  • 37
1

Instead of this -

ft.replace(R.id.homepage_container, timeline);

Use this -

ft.add(R.id.homepage_container, timeline);

Now the fragment below will be alive. And if you don't want to unintentionally click the fragment below, add to currents fragment layout top container-

android:clickable="true"
Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48