Android - Fragments return to their first state after being replaced via the FragmentManager

I have an activity that has 3 tab buttons and each one will bring its own snippet to the screen. And one of them has a music player and image buttons that change their icons when I click them. (for example, changing the play button is possible to pause)

The problem is when I click that image play button and change tabs and return the tab using the music player, the image button is reset. I think the fragmentmanager.replace method creates a new instance of the 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


source to share


3 answers


I think the problem is that you are replacing all the chunks of time that are in the manager. Try to control 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);

      

+1


source


From Android Developers :

When deleting or replacing a fragment and adding a transaction to the back stack, the deleted fragment is stopped (not destroyed). If the user goes back to restore the chunk, it will restart. If you don't add the transaction to the back stack, then the chunk is destroyed when removed or replaced.

So, if you call ft.addToBackStack () it shouldn't destroy your fragments.



Register the fragment lifecycle methods to get a better understanding of what's going on.

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

      

And let's do the same for onPause,

+1


source


Instead of this -

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

      

Use this -

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

      

The snippet below will now be live. And if you don't want to inadvertently click the snippet below, add to the tag the layout snippet of the top container-

android:clickable="true"

      

+1


source







All Articles