其他分享
首页 > 其他分享> > Android-片段通过FragmentManager替换后返回其第一状态

Android-片段通过FragmentManager替换后返回其第一状态

作者:互联网

我有一个带有3个选项卡按钮的活动,每个按钮都会将自己的片段显示在屏幕上.而且其中一个fragmnet具有一个音乐播放器和一个图像按钮,当我单击它们时,它们会更改其图标. (例如将播放按钮更改为可绘制以暂停)

问题是,当我单击此播放图像按钮并更改选项卡并返回带有音乐播放器的选项卡时,图像按钮将重置.我认为fragmentmanager.replace方法会创建一个fragment的新实例.我如何避免这种情况.

这是我的代码段.

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

解决方法:

我认为问题在于您要替换经理中的所有时间片段.尝试控制可见片段,并使用以下代码:

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

标签:fragmentmanager,android-fragments,fragment,android
来源: https://codeday.me/bug/20191120/2042334.html