Reputation: 146
how can i refresh the view of a fragment, when the back button is pressed?
I've tried this in the onResume method of the fragment but it doesn't work.
Ok, here is the code
the layout look like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/mainscreen_bg">
<fragment
android:id="@+id/topbar"
android:name="de.app.applib.TopbarFragment"
android:layout_width="match_parent"
android:layout_height="@dimen/topbar_height"
android:layout_alignParentTop="true"
tools:layout="@layout/fragment_topbar" />
<fragment
android:id="@+id/position_list"
android:name="de.app.applib.CriteriaListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@id/topbar"
android:tag="CriteriaList"
tools:layout="@layout/fragment_criterialist" />
The TopbarFragment has the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/topbar_bg" >
<ImageView android:id="@+id/topbar_home"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:src="@drawable/ic_ab_back_holo_light"
android:visibility="invisible"
android:padding="0dp"
android:contentDescription="@string/none"
android:layout_margin="0dp"/>
<ImageView android:id="@+id/topbar_logo"
android:src="@drawable/actionbar_logo"
android:layout_height="30dp"
android:layout_width="120dp"
android:layout_centerVertical="true"
android:padding="0dp"
android:layout_margin="0dp"
android:contentDescription="@string/none"
android:layout_toRightOf="@id/topbar_home"/>
<ToggleButton
android:id="@+id/topbar_btn_push_abo"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/topbar_btn_info"
android:layout_toLeftOf="@id/topbar_btn_info"
android:background="@drawable/topbar_btn_push_bg"
android:paddingTop="@dimen/padding_topbar_btn_push"
android:textOff=""
android:textOn=""
android:textSize="12sp" />
In the onCreateView() of the Fragment i set the textOn and textOff values of the toggleButton.
This Fragment is included in all activities.
Now, i want to update the text of the toggleButton when the user pressed the back button. I set the new text in the onResume() Method of the Fragment. This Method is called but the old text appears in the activity and not the new updated text.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG,"onCreateView called");
View v = inflater.inflate(ResourceUtils.getCustomizableLayoutID(getActivity(), "fragment_topbar"), container, false);
subscrButton = (ToggleButton) v.findViewById(R.id.topbar_btn_push_abo);
setCountOfNewAdds();
private void setCountOfNewAdds() {
int countOfNewAds = ((App)getActivity().getApplication()).getCountOfNewAds();
subscrButton.setTextOff(countOfNewAds>0?""+countOfNewAds:"");
subscrButton.setTextOn(countOfNewAds>0?""+countOfNewAds:"");
subscrButton.invalidate();
}
@Override
public void onResume() {
super.onResume();
setCountOfNewAdds();
}
Upvotes: 3
Views: 10298
Reputation: 28773
In my case I was receiving user location using LocationManager
. First I opened another fragment (fragment B) and then returned to current (fragment A) with Back button. Then passed view
variable (view == getView()) to a method that started locationManager.requestSingleUpdate
.
After receiving onLocationChanged
event that view
variable oddly changed to another value, while getView()
was right. So, when I later accessed any control with view.textview.text = "aaa"
it didn't set new value, and the fragment didn't refresh. Also getView()
is not always not null
, it can be null
, if you get it from onCreateView
, but not from onViewCreated
.
Upvotes: 0
Reputation: 146
the Solution for the Problem was to use Framelayout in the layout to include the fragment e.g.
<FrameLayout
android:id="@+id/topbar"
android:name="de.app.applib.TopbarFragment"
android:layout_width="match_parent"
android:layout_height="@dimen/topbar_height"
android:layout_alignParentTop="true"
tools:layout="@layout/fragment_topbar" />
and then use the FragmentManager to insert the Fragment in the onStart() Method
@Override
protected void onStart() {
super.onStart();
TopbarFragment topbarFragment = (TopbarFragment) getSupportFragmentManager().findFragmentById(R.id.topbar);
if(topbarFragment == null) {
topbarFragment = new TopbarFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.topbar, topbarFragment).commit();
getSupportFragmentManager().executePendingTransactions();
}
}
then the update in the onResume() Method works
Upvotes: 1
Reputation: 3080
You can capture the users back click in the activity that the Fragment is hosted in.
Then, you can call a refresh method in the Fragment to handle refreshing your view.
In your activity:
@Override
public void onBackPressed() {
Fragment yourFragment = getSupportFragmentManager().findFragmentById("ID");
(TopbarFragment) yourFragment.setCountOfNewAdds();
}
Upvotes: 1
Reputation: 241
If your fragment call replace()
, not add()
method to switch to another fragment (SecondFragment), your fragment will be called again the onCreateView()
method when the back button in SecondFragment is pressed
Upvotes: 3