Я хочу вызвать метод фрагмента из его родительской активности. Для этого я хочу объект фрагмента.findFragmentByTag возвращает null в android
родительская активность имеют фрагмент в framelayout так:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bottom_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
Это код для получения объекта фрагмента.
FragmentBottomButtons fragment = new FragmentBottomButtons();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.bottom_buttons, fragment,"FragmentTag");
//ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
//ft.addToBackStack("");
ft.commit();
/*
getSupportFragmentManager()
.beginTransaction()
.add(R.id.bottom_buttons, new FragmentBottomButtons())
.commit();
*/
frag = (FragmentBottomButtons) getSupportFragmentManager().findFragmentByTag("FragmentTag");
//fragmentBottomButtons = (FrameLayout)findViewById(R.id.bottom_buttons);
if (frag == null){
Utility.displayToast("fragmnt is null");
}
Но он возвращает null.
Может ли кто-нибудь помочь мне в этом? что здесь не так?
Попробуйте выполнить fragmentManager.executePendingTransactions(); перед тем как получить фрагмент, посмотрите, работает ли это. commit() работает асинхронно и его можно вызвать, прежде чем действие будет полностью завершено. – aminner