FragmentManager.executePendingTransactions不会立即调用onCreateView()

时间:2016-03-20 10:34:21

标签: android android-fragments

我目前正在尝试以编程方式添加一个片段,该片段具有我需要在片段创建后立即初始化的视图。我试图在FragmentManager.executePendingTransactions()之后立即使用FragmentTransition.commit(),但它并没有像预期的那样工作。我已经进行了研究并找到this,因此我意识到FragmentManager.executePendingTransactions()无法使用onCreate(),然后将代码放到onStart(),但它仍然没有工作。虽然在我的情况下它是从onItemClickListener()打来的,但我认为它没有任何区别。这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vk_music_list);
    musicProgressBar = new MusicProgressBar();
    listView = (ListView) findViewById(R.id.listView);
}

@Override
protected void onStart() {
    super.onStart();
    listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String audioUrl = ((VKApiAudio) parent.getAdapter().getItem(position)).url;
    serviceIntent.putExtra("audioUrl", audioUrl);

    //Inflate music progress bar
    fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragment = fragmentManager.findFragmentById(R.id.musicBarContainer);
    if(fragment == null) {
        fragmentTransaction.add(R.id.musicBarContainer, musicProgressBar)
                .setTransition(R.anim.move_from_the_bottom)
                .commit();
    }

    //This guy doesn't work
    fragmentManager.executePendingTransactions();

    //Never get into this, because fragment is always == null
    if(fragment != null) {
        SeekBar seekBar = (SeekBar) fragment.getView().findViewById(R.id.progressBar);
        seekBar.setMax(((VKApiAudio) parent.getAdapter().getItem(position)).duration);
    }
    startService(serviceIntent);
}

1 个答案:

答案 0 :(得分:2)

好吧,我终于找到了问题所在。问题的原因是fragmentTransaction.commit()fragmentManager.executePendingTransactions()都没有更改fragment变量中存储的引用。因此,在executePendingTransactions()之后我需要再次调用findFragmentById(),因此它存储的引用将引用创建的片段。这是一个有效的代码:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String audioUrl = ((VKApiAudio) parent.getAdapter().getItem(position)).url;
    serviceIntent.putExtra("audioUrl", audioUrl);

    //Inflate music progress bar
    fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragment = fragmentManager.findFragmentById(R.id.musicBarContainer);
    if(fragment == null) {
        fragmentTransaction.add(R.id.musicBarContainer, musicProgressBar)
                .commit();
    }

    //Force to create fragment without scheduling so it's not necessarily to wait until it get scheduled
    fragmentManager.executePendingTransactions();

    //This is what I needed
    fragment = fragmentManager.findFragmentById(R.id.musicBarContainer);

    //Set song duration as a seekBar's maximum
        if (fragment != null) {
            SeekBar seekBar = (SeekBar) fragment.getView().findViewById(R.id.progressBar);
            seekBar.setMax(((VKApiAudio) parent.getAdapter().getItem(position)).duration);
        }
    startService(serviceIntent);
}

我希望它会对某人有所帮助。