问题
What I am doing: updating UI (Fragment UI) from Broadcast Receiver. But while finding Fragment I am getting null in return.
OnReceive (Broadcast Receiver):
if(MainActivity.getInstace()!=null){
MainActivity.getInstace().updateUI();
}
MainActivity.class (Fragment Activity):
public void updateUI() {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
//getting null here
SlidingTab frag = (SlidingTab)getSupportFragmentManager().findFragmentByTag("slidingtab");
frag.updateUI();
}
});
}
Code for calling Fragment from Activity:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
ft.setCustomAnimations(R.anim.glide_fragment_horizontal_in, R.anim.glide_fragment_horizontal_out);
ft.replace(R.id.content_frame1, new SlidingTab(), "slidingtab");
ft.addToBackStack("slidingtab");
// Start the animated transition.
ft.commit();
break;
SlidingTab.class (Fragment UI update code ):
public class SlidingTab extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.sliding_tab, container, false);}
public void updateUI(){
badge.setVisibility(View.VISIBLE);
}
}
Fragment (SlidingTab.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
tools:context="com.RareMediaCompany.BDTrial.SlidingTab">
<include
android:id="@+id/toolbar1"
layout="@layout/toolbar_job" />
<com.RareMediaCompany.BDTrial.Utils.CustomTabLayout
android:id="@+id/sliding_tabs"
style="@style/CustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eeeeee"
app:tabIndicatorColor="#f39220"
app:tabIndicatorHeight="3dp"
app:tabMaxWidth="0dp"
app:tabMode="fixed"
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp"
app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
app:tabSelectedTextColor="#808080" />
<LinearLayout
android:id="@+id/linear1"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginRight="8dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
android:weightSum="1">
<android.support.v7.widget.SearchView
android:layout_width="300dp"
android:layout_height="45dp"
android:id="@+id/searchView"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginEnd="5dp"
android:clickable="true"
style="@style/CitySearchView"
android:background="@drawable/searchview"
android:layout_marginLeft="5dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:weightSum="1"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/list_linearlayout"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="0.5"
android:layout_gravity="center"
android:background="#f39220">
<!--android:background="#75aadb">-->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_weight="0.3"
android:src="@drawable/listicon"/>
</LinearLayout>
<LinearLayout
android:id="@+id/maplist"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="0.5"
android:background="#75aadb">
<ImageView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginRight="3dp"
android:layout_weight="0.3"
android:src="@drawable/map_icon_1"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!--android:layout_width="320dp"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginLeft="10dp"-->
<!--android:layout_marginTop="10dp"-->
<!--android:id="@+id/searchview"/>-->
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/white" />
</LinearLayout>
The fragment (SlidingTab) consist of textviews and viewpager . And i
am not able to find it in MainActivity.class. Every time i callSlidingTab frag =
(SlidingTab)getSupportFragmentManager().findFragmentByTag("slidingtab")
Frag comes null.
回答1:
Use getSupportFragmentManager().executePendingTransactions() after fragment transaction. After calling this method you can get the fragment using both findFragmentById() and findFragmentByTag() methods.
Also make sure you pass Valid Id or Tag for the fragment.
来源:https://stackoverflow.com/questions/37806667/getsupportfragmentmanager-findfragmentbyid-returning-null