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