[英]Fragment doesn't update values from other fragment
我是android的新手,我的目的是將此值傳遞給一個片段,它是我當前的片段。 但是我想更新片段 ,我在另一個片段中有此數據。
當我執行此操作時,我的捆綁包將轉到片段。 即使我可以在不停止應用程序的情況下在textView中執行setText,它也不會更改。
private void passToScreen(String title, String artist, String album, Long duration) {
bundle.putString("songTitle",title);
bundle.putString("songArtist", artist);
bundle.putString("songAlbum", album);
bundle.putString("durationSong", duration.toString());
mActivity.getSupportFragmentManager()
.beginTransaction()
.detach(songScreen)
.commitNowAllowingStateLoss();
songScreen.setArguments(bundle);
mActivity.getSupportFragmentManager()
.beginTransaction()
.attach(songScreen)
.commitAllowingStateLoss();
})
我做錯了什么?
先感謝您。
如果該片段已經可見,則可以使用FragmentManager獲取該片段的實例:
MyFragment myFragment = (MyFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment_or_container_id);
myFragment.updateViews(bundle);
然后在片段類中創建方法以更新視圖:
public void updateViews(Bundle bundle) {
//update the views
}
您無需分離片段。 請注意,這僅在片段以“提交”過程完成時才起作用。 commit()和commitAllowingStateLoss()函數異步工作,因此,如果在commit()之后立即調用findFragmentById(),它將返回null。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.