why viewPage keeps previous viewpage content after clicking back button - android

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.

Related

Why does not `replace` replace a fragment?

I have the following code that adds fragments to the same container R.id.container. First I add one fragment, then after user interacts with it, I add another one with the same code
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
My question when I cell getSupportFragmentManager().getFragments() I can see two fragments. Why doesn't replace actually replace but add?
It keeps the two fragments because
addToBackStack(null);
means the user is able to revert back to the previous state.
If you add fragment1 (using replace), then fragment2, only fragment2 is shown, but if the user press back, the FragmentManager needs to show fragment1, so it keeps a reference to both fragment.

FragmentTransaction add() behavior

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.

back stack and statically added fragment

I have a fragment added statically from XML I want to replace this fragment by another fragment, I did that by adding this code:
CFragment singleStationFragment = new CFragment();
android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.layoutlist, singleStationFragment);
transaction.addToBackStack(null);
transaction.commit();
the problem is that when I press the back button the first fragment is not shown because it was not added through a transaction and the manager doesn't know about it, is there a way I could add the first fragment (ALREADY ADDED FROM XML), to my backstack or I could just show it when I click back instead of exiting the app ? Thanks !
As far as I'm aware you will have to add your first fragment to the activity in code rather than in the layout file. Do this with the add method of FragmentTransaction
transaction.add(R.id.FragmentContainer, fragment);

Android Fragment Unable to go back

I have an app that uses fragments with tabs/viewpager
[Tab 1][Tab 2][Tab 3]
Tab2 has a ListView and in the onClick method of the ListView I am showing a detail view with the following code
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
NextFragment nextFragment = new NextFragment();
transaction.replace(R.id.container, nextFragment);
transaction.addToBackStack(null);
transaction.commit();
The issue is that in the NextFragment when I use the hardware back button the whole app closes?
I'm not sure why it doesn't let you return when back is pressed, but what I've done in my application is the following:
I have overriden the onBackPressed action and created a check that once it is pressed and the user is in the fragment that he wants to return from, just create another fragment transaction to the previous fragment.
I have done so because I needed to update the previous fragment, but I'm sure there is a better way to solve your problem.

Android fragment unexpectedly close

I use a holder activity with FrameLayout.
There I put a fragment with a listview. It works fine.
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragments, feedFragment);
ft.commit();
Then I add another fragment.
android.support.v4.app.Fragment targetFragment = new MainPhotoFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragments, targetFragment);
ft.addToBackStack(null);
ft.commit();
Here I use add() instead of replace() to return to previous position of the listview when hitting back key. It works fine.
But it is possible to navigate to the third fragment from the second fragment.
android.support.v4.app.Fragment targetFragment = new FullPhotoFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments, targetFragment);
ft.addToBackStack(null);
ft.commit();
Here I use replace to force the 2nd fragment to reload when hitting back key.
Sometimes back key from the third fragment works fine, it displays the second fragment that is reloading on appearing.
But sometimes (as I can see it happens first time when I try this steps) hitting back key from the third fragment leads me to the first fragment, closing the second fragment against my expectations. And the first fragment is reloading.
How to prevent this strange behavious?
add() method will add Fragments to Container and any other fragments added to the Container will be queued back of the first fragment. They will be not visible until and unless first fragment made Invisible. I hope this is the problem you are facing. It would be good if you use replace() for the first-->second fragment navigation also.

Categories

Resources