I have log in button in shopping cart fragment. When this button is clicked, it redirects to sign in fragment. Also I have a button to login in sign in fragment. When this button is clicked, getSupportFragmentManager().popBackStackImmediate(); method is called. I want to refresh my shopping cart fragment on this method call.
How can I do it?
For refreshing fragment you can override setUserVisibleHint method into your fragment. Whenever your fragment is visible on screen this refreshData method is call.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
RefreshData() ;
}
}
You can refresh the shopping cart Fragment when it becomes visible again by overriding ShoppingCartFragment.onStart(). See the lifecycle of Fragments.
public class MyFragment extends Fragment {
#Override
public void onStart() {
super.onStart();
updateUi()
}
}
You can enforce that the ShoppingCartFragment is recreated when the back stack is popped by using FragmentTransaction.replace() when showing the LoginFragment.
Related
see my application view here
I have 3 Fragments that I switch between with three Toolbar buttons. When I go from 1st fragment to 2nd fragment onPause() or onStop() methods are not called and android sees that fragment as still running I guess, but when I go from 1st fragment to 3rd, they are called. I want it to be called when I go from 1st to 2nd one, so I can call onResume() function when I go back to 1st fragment, so I can trigger some functions. What causes it and how can I solve?
Fragment's LifeCycle methods( onPause() and onStop() etc.) Dependent on Activity's Life cycle Methods.
It means if your Activity's onPause() and onStop() called then your fragment's onPause() and onStop() will be called
If you want to notify your fragment that it is visible or not then you can use.
Override this method in your fragments.
#Override
public void setUserVisibleHint(boolean isVisibleToUser)
{
super.setUserVisibleHint(isVisibleToUser);
if (visible)
{
// Control will be here if fragment is visible.
//Do whatever you want.
}
}
These are fragments inside ViewPager. so, on stop or onpause wont call
there is a interface in fragment named as
#Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
// ...
}
}
using this method u can get to know the visibility of fragment
else you can use the setOffscreenPageLimit(value);
example
mViewPager.setOffscreenPageLimit(1);
I am using a FragmentStatePagerAdapter to create a SlidingTabLayout with 3 tabs. I want to do something each time a Fragment is viewed on my screen. I've tried putting what I want to happen inside the onStart() of the respective fragment like so:
public void onStart() {
//Do This
}
but it only works when the Fragment is moved 2 fragments away (i.e. 1st to 3rd, 3rd to 1st). Is there a way to do something each time a fragment is viewed on screen?
you can override setUserVisibleHint()
public class MyFragment extends Fragment {
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// here fragment is visible to user
}
else {
}
}
}
I have a below situation:
An activity, with a Fragment-A.
On user input, a new Fragment (lets call it Fragment-B)is added, by using below code:
ft.add(R.id.content_frame, fragmentB);
Now, when I press back, the fragment-B is destroyed as expected.
As fragment-B was added(and not replaced) there is no callback to onResume() of Fragment A.
I need to set focus of fragment-A as soon as fragment-B is destroyed.
I use below code to set the Focus:
getView().setFocusableInTouchMode(true);
getView().requestFocus();
Is there any way to achieve this?
For those developers who are struggling to get the "callback" in Fragment( when fragments are added and NOT Replaced ), can try below method:
Use addOnBackStackChangedListener which adds a new listener for changes to the fragment back stack.
I added this addOnBackStackChangedListener in onCreate() method of my Fragment-A, as below:
getFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if (getView()!=null){
getView().setFocusableInTouchMode(true);
getView().requestFocus();
}
}
});
Now As soon as Fragment-B is destroyed, onBackStackChanged is called and I was able to set the focus.
Of course, you can write whatever code you want to execute inside onBackStackChanged when Fragment is destroyed.
Create an interface:
public interface DestructionListener {
void onDestroyed();
}
Now in your FragmentB class put an implementation of Subject:
private DestructionListener listener;
public void setDestructionListener(DestructionListener listener) {
this.listener = listener;
}
Still in the same fragment, apply such change:
#Override
public void onDestroy() {
...
if(listener != null)
listener.onDestroyed();
super.onDestroy();
}
Now you have Listener pattern implemented - you can listen for an event which occurs when FragmentB is being destroyed. In your activity, just put:
fragmentB.setDestructionListener(new DestructionListener() {
void onDestroyed() {
// Request focus here
}
});
and you're good to go.
You can simply hide the fragment(in which you dont want focus) and show the one u want focus since you are adding fragments and not replacing.
I have a Activity that transitions between fragments. Fragment 1 to fragment 2. Fragment 1 has a listview. When the user goes to fragment 2 the user has the option to make changes to the data that fills the listview. When the user presses the back button it takes them back to the first fragment. When the user returns the listview has to be updated to show the changes that were made in fragment 2. How can i do this?
Frag2 pfrag = new Frag2();
pfrag.setArguments(bundle);
ft=fm.beginTransaction();
ft.add(R.id.fragment_swap, pfrag,"Profile");
ft.show(pfrag);
ft.addToBackStack("pfrag");
ft.commit();
You would do this by calling notifyDataSetChanged(); on the ListView's adapter.
For more info
You could use Robins answer or if you want to pass the data in fragment 2 to fragment 1 you could call a method in fragment 1 class:
YourFragmentClass.refreshList( your data)
and in Fragment 2 the method:
puplic void refreshList(your data) {
// set the adapter with your data
}
In you fragment, include the code as like below.
(isPaused)boolean variable is used to track current fragment is paused while navigating to next activity.
While resuming back call the webservice (or) call the method to refresh the list by calling notifydatasetchanged() from adapter.
#Override
public void onResume() {
super.onResume();
if(isPaused) {
isPaused=true;
fetchDirectories();
// call the adapter method to notify the data set.
}
}
#Override
public void onPause() {
super.onPause();
isPaused=true;
}
private boolean isPaused=false;
I think the best way to achieve this, is to load the list in the fragment's onResume()-Method. So, the code looks something like this:
#Override
public void onResume() {
super.onResume();
initializeList();
}
...
private void initializeList() {
adapter = new MessageTemplateAdapter(getActivity(), datasource.getTemplateList());
setListAdapter(adapter);
}
In my application, I have a ViewPager which holds many swipeable Tabs with Fragments inside. Is there a method like onResume which is called every time the fragment comes to the screen? onResume, onCreateView and so on are called after the Fragment is created and not if it comes to the screen, so they do not work for me. Which method can I use for my problem?
You can use setUserVisibleHint method in your fragment:
#Override
public void setUserVisibleHint(boolean isVisibleToUser){
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
// Your fragment is visible
}
}