Update viewpager fragment specific position and get back to previous while backpress - android

I have a view-pager with two fragment in it . based on my logic i want to replace the fragment on a specific position with another fragment but when user back-press i want to go back to previous fragment i replaced with it
for clearance
i have 4 fragment A, B, C, D initially i added A and B to view-pager, then i replaced B with c in view-pager , So that i want is to go back to fragment B (replaced by C) from C (current) while pressing back button instead of going back to another activity

you can do using assign constant variable to your fragment or assign tag at time of Fragment Call.
I manage using VariableName CurrentFragment which update value on Fragment Lunch. Then after onBackpress method of your activity i handle the fragment based on Constant Variable.
Visit below Stackoverflow link where i put solution step-wise to handle multiple fragment on backpress.
Click Here to View StackOverFlowAnswer

Related

How to use navigation arguments when using three fragments in a row android kotlin

There is such a task:
I have three fragments: A, B, C
There is navigation between them:
A -> B -> C
Fragment A contains a RecyclerView, when you click on any of them, you go to Fragment B, where the id of the pressed element is passed through arguments.
Everything seems to be fine, but there is a problem. In Fragment B, I display information according to the id of the selected element, however, if I then go from Fragment B to Fragment C, and then back to Fragment B from Fragment C, I will get an error, since such an argument no longer exists.
How to be in such a situation?

How to move activity to another activity with specific fragment target?

Is there a way that from the activity i want to go to another activity with multiple fragment and select a fragment that i want to focus it.
Assuming you want to move from Actvity A to Activity B and make Activity B show a particular fragment. You need to pass an extra containing some form of identity(Eg. the name you gave to your fragment) of the fragment you want to show to Activity B. Then in onCreate in Activity B read the extra you passed to it to determine which fragment to setup in Activity B

Cannot update fragment's data (fragment in viewpager)

I have 3 activites- A,B, and C. All these 3 activities have a viewpager. And these viewpagers have 1 fragment(this fragment is common to all viewpagers). The launcher activity is A, and pressing a button in A takes me to B, and pressing a button in B takes me to C.
A-> B -> C
Now the problem is when I update any data in C's fragment, it is not updated in A's and B's fragment. But if I update any data in A's fragment and then go to B, B's fragment will have updated data but vice versa is not happening.
Note: I' using a viewpager because I'll be adding more fragments in the future.
I assume the reason this is happening is because when C's fragment is updated, A's and B's fragment is having the old instance of the viewpager (or fragment). So, when I come back to A or B, the old data is being shown.
If this is the problem what is the best solution?
I have tried using viewpager.notifyDataSetChanged(),this works but messes up the views in the fragment.
Can anybody suggest an alternative?Thank you in advance!

Hide all Fragments in Backstack but still being able to go back

I have a small layout in my activity that I add Fragments to based on the User navigating through the app.
Assuming the user navigates thusly:
Activity -> Fragment A -> Fragment B -> Fragment C -> Button Click
I would like to be able to to hide the Fragments and show the blank Activity again.
This is how I'm adding the Fragments to the activity:
protected void addFragment(Fragment fragment)
{
getSupportFragmentManager().beginTransaction().replace(R.id.secondary_fragment, fragment).addToBackStack(fragment.getTitle()).commit();
}
To clear all the Fragments, I use:
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
However, is there a way to clear the fragments in a way that if the user presses back, they would be able to go back to Fragment C (as opposed to exiting the App)?
Maybe instead of pop all the backStack, you just get the fragment view by id and setVisibility to invisible?
Try starting a new instance of your Activity with a clear stack on the button press (if I'm correct in assuming this comes after C as you described). This way the First Activity instance will still have up to Fragment C and the Second Activity instance will be whatever you like (Fragment A > Fragment D > Fragment F). And you won't need to pop/clear any back stack for any Activity.
HTHs

Knowing when a fragment is left from / returned to

Android's Fragment's onResume/onPause methods are tightly coupled with the host Activity's lifecycle as shown here.
What I want to know is how to detect that a fragment is left from / returned to inside the app's navigation flow.
Example:
Say I have MainActivity and fragments A,B and C.
MainActivity adds fragment A, then B and then C.
How do I know fragment B was left (I now see fragment C).
Also, once I press on back, how do I know fragment B was resumed?
Edit:
Clarification: I want to know that from within fragment B (similar to the way an Activity works with onPause and onResume)
Try isDetached() method link here
Respectively there is isAdded()
Indirectly you question concern with Fragment LifeCycle.
And you can trace fragment navigation replace,remove,add using Fragment manager
PopBackStack : Pop the last fragment transition from the manager's fragment back stack. If there is nothing to pop, false is returned.

Categories

Resources