I am using mViewPager.setOnPageChangeListener to detect when a user navigated to a specific page. Each time he navigates to this specific page, I have to reload the Fragment or return it to its original state.
I tried using replace but the app breaks on the first parameter which is the ViewPager itself.
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment fragment = new MyFragment();
fragmentTransaction.replace(R.id.viewPager, fragment);
fragmentTransaction.commit();
I tried to use the adapter itself, but I was brought to a dead end.
Where am I making a mistake? How can I reload the fragment each time a user navigate to a specific page?
Related
I have a problem when I click back button while using ViewPage. When I enter to ViewPage after going out and coming back, it shows me the content of previous ViewPage, not new ones.
I'm entering to ViewPage from a RecyclerView and I want to change ViewPage content by clicking on items of RecyclerView.
So my problem is that why ViewPage is not reset to new content?
before committing the fragment do this! it won't remember the last open fragment and please read about BackStack management in fragments here
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
If by saying ViewPage you mean 'ViewPager'. Then you must be displaying the content using a fragment. So when you open the ViewPager screen 'Replace' the fragment with new content.
Use this syntax:
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new Frag_four()).commit();
Or this one
getFragmentManager().beginTransaction()
.replace(R.id.container, new Frag_four()).commit();
You are facing this issue because may be you are not removing previous content from the container. So just give it a try.
EDIT:
In your fragment displaying activity place the following code in 'OnDestory' method.
getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.your_container)).commit();
This will empty the container. So next time it will load the new fragment.
I wanted to know how I can get the Fragment which is onloaded on my Acticvity.
The background behind this is, that I want to change the onBackPressed method that it's switching to the right fragments. At the moment when I press "Back" the app closes, because I work alot with fragments.
Use addToBackStack on your fragment transaction. This way when you press the back key the transaction gets rolled back and thus your fragment disappears.
I got a solution for the problem:
Fragment fragment = new Fragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.Layout,Fragment);
fragmentTransaction.addToBackStack("flow1");
fragmentTransaction.commit();
On navigating from one fragment to another which are part of same activity, I am using slide animation.
WebFragment fragment = WebFragment.newInstance(Globals.TGURL_CREATE_ACTIVITY, "");
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.setCustomAnimations(R.anim.enter_anim, R.anim.exit_anim, R.anim.enter_anim, R.anim.exit_anim);
transaction.replace(R.id.fragment_activity_layout, fragment);
transaction.addToBackStack(null);
transaction.commit();
This code ensures that when I come back to the first fragment, animation is there.
Well this has been a boon for me so far. But in one specific case it is becoming a curse. The activity is placed on ActionBar tab. When the 2nd (WebFragment) is the current one, and I tap the tab and not the back button, I want the first fragment to be displayed without any animation.
But it has been impossible for me to do this so far as the navigation is inheriting the animation which was earlier given.
This is what I am doing for going back :
TabActivity.this.getSupportFragmentManager().popBackStack();
Working with fragments I've always used replace() for my transactions, but I wish I didn't have to save instance states anymore to restore a fragment's view and prevent reloading when coming back to that fragment. So, I've decided to work with add(). The thing is when I add another fragment, the previous fragment view remains in the background and that's fine (that's the behavior I expected), but the problem is I can actually interact with the views in the background. Example:
Fragment A has a Button
Fragment B has a TextView
When I add Fragment A and later add Fragment B, I'm able to click on Fragment A's Button, even staying on Fragment B's view.
I'm using:
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction().
add(getRootViewContainer(),fragment,fragment.getClass().getSimpleName());
if (shouldGoBack)
fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
where getRootViewContainer() returns the id of the FrameLayout I'm using as my activity main container.
Now, is it really the default behavior of add()?
If so, is there a proper way to avoid this or one just has to use replace()?
What you can do here is just hide previous fragment at the time of transaction of current fragment.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment newFragment= new MyFragment ();
ft.hide(CurrentFragment.this);
ft.show(newFragment);
ft.commit();
It worked for me just try it.
FragmentTransaction.hide(fragmentBehind); //works for me!
example :
//I have it globally available
FragmentTransaction trans = MainActivity.getManager().beginTransaction();
//not globally
FragmentTransaction trans = getFragmentManager().beginTransaction();
MapFragment newFragment = new newFragment();
trans.add(R.id.fragmentContainer, newFragment, tag);
trans.hide(this);
trans.addToBackStack(tag);
trans.commit();
Yes, this is a default behaviour of add().
If you really don't want to user replace(), you can try to disable views which are inside "old" fragment.
My app is using FragmentTransaction to navigate across screens. When i want to go back to the previous fragment using back button, the content of both current and previous fragment merge, and the result is pretty ugly.
For the record, here's my Transaction code :
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, new MyFragment());
ft.addToBackStack(null);
ft.commit();
Thanks