Android Fragment onActivityResult go to next fragment with data from previous fragment? - android

A have a fragment (Fragment A) which gets a selected photo from a user. Once the user selects the photo in Fragment A I want to go to Fragment B and take the selected photo with me. I am also using viewpager so the user can swipe between the fragments...I want the user to be able to choose the photo and in the onActivityResult in Fragment A go to or start Fragment B. Is this possible?

You will need to use a listener (ie. FragmentA.onFragmentBListener) to communicate between fragments.
In Fragment B, you will trigger an action, and listen for it in FragmentA (which can then act on the event, by starting, or doing anything).
Here is a good answer:
Android: correct way of jumping between fragments

Related

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

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

Save Fragment Data onreplace and fill in after popbackstack

I have FragmentX in a ViewPager. FragmentX has EditText's in it and a Button. The user presses the Button and FragmentY replaces FragmentX. The user then presses back and FragmentX has lost all of its input from the user.
How do you either:
a) Save the data in FragmentX before FragmentY appears then FragmentY is replaced by FragmentX retrieve the data and fill in the EditText's in FragmentX
(please don't reply with OnSaveInstanceState, as this does not work unless the Fragment is destroyed, which in this case it is not)
b) somehow keep the data in FragmentX so it is there when we go back to FragmentX from FragmentY..
Any suggestions?
Using addToBackStack() might help in your case.
If you return to a fragment from the back stack it does not re-create the fragment but re-uses the same instance and starts with onCreateView() in the fragment lifecycle, see Fragment lifecycle.
So if you want to store state you should use instance variables and not rely on onSaveInstanceState().
Check this out: Maintaining fragment states
I am now saving the data in FragmentX to SharedPreferences and overriding onBackPressed in the Activity, and have created a function in the Activity called popFromBackstack where the popBackStack() occurs.
In the functions in my Activity where i replace my Fragments i am now saving the data in FragmentX into SharedPrefs before the replace. I am also checking after the replace if the new Fragment is FragmentX and, if it is, i am filling the data into FragmentX from SharedPrefs.
I am also saving the data from FragmentX in onBackPressed in my Activity (if the current fragment is FragmentX), incase the user presses the back button.
I have also created a public static activity called popBackStack() in my Activity which i call from Fragments to pop the backstack. I am also saving the data from FragmentX here (if the current fragment being popped is FragmentX). Once the Fragment being popped is popped i am checking if the new Fragment is FragmentX, and filling in the data if it is...
Long winded approach but i couldn't figure out any other reliable way. This is working perfectly.
I suggest following guidelines that Google provides and implement an interface declared in your Fragment and save the Bundle or whatever object you want in the activity. Then, in your newInstace() static factory method pass that Bundle and recreate data as usual. Since you are using a ViewPager and it will always render the second fragment before the button is pushed (I assume your second fragment is in another tab) you still need to manage it via an interface. When the back button is pressed, the data will still be there, unless it is destroyed, and you still need to implement onSaveInstaceState() for that matter. You can also use setRetainInstance(boolean retain). See here for more details

Android: How can you pre-populate the fragment backstack?

I have an app that has one Activity that loads fragments… on initial startup it loads fragment A. From A the user can navigate to fragment B; and from B navigate to fragment C. Each time a fragment is replaced I do addToBackStack. The back button navigates as one would expect, C to B to A, and if you press back again the app exits.
I’m also using a Dropbox datastore to save all my app’s data so I can move seamlessly between devices and have all my data synced. This seems to be working well. As part of my data I store which fragment was last displayed. Now when my app starts on a second device it correctly displays the most recently opened fragment. This works fine, however, I no longer have a backstack. For example, if the app is showing fragment C device one and then I start the app on device two, as expected fragment C is shown on device two. But when I press the back button, the app exits instead of showing fragment B. (Which makes sense since on device two the app only loaded fragment C.)
So my question: How do I pre-populate my baskstack such that when pressing the back button on device two that it navigates to fragment B?
Check out TaskStackBuilder or the activity XML declaration android:parentActivityName.
I solved this by just committing multiple fragment transactions. Works for me
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment1(), "f1").addToBackStack(null).commit()
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment2(), "f2").addToBackStack(null).commit()
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment3(), "f3").addToBackStack(null).commit()
supportFragmentManager.beginTransaction().replace(R.id.content, Fragment4(), "f4").commit()
However, for a cleaner and better solution you could also have a look at the Jetpack Navigation UI

How can return back to specified fragment in another activity in android?

I have four fragments in one viewpager of Activity1, In any of fragment1~4, I open another activity2 by startactivity(Intent), In activity2, I want to return back to previous fragment or other specified fragments, but Now I always return back to the first fragment1 by startactivity(), Any good ideas ? Thanks
You can access the current 'page' and set the current page like this. I guess you could bundle this information when you start activity2 through the intent. Note "FirstFragment" is the name of the activity that sets up my view pager etc with fragments.
int currentviewnum = ((FirstFragment) getActivity()).getPager().getCurrentItem();
Once, in activity 2 and you want to return back to it, or another view pager (currentviewnum), you could do something like this in an override for the back button (see here for that Android - How To Override the "Back" button so it doesn't Finish() my Activity?); But you can do something like this!
((FirstFragment) getActivity()).getPager().setCurrentItem(currentviewnum);
So effectively when you press the back button, let me know if this works. I do something like this in my app. But instead of going back, it's more to go to the next one!

Handling to reopen fragment on back press

Let me explain my application first.
I have an activity and fragments in it. I'm opening fragmentA onCreate of application. In fragmentA,before i open fragmentB by using support fragment manager's replace() method, i call a webService for some json datas.
Then i open fragmentB with these json datas. After i opened the fragmentB, i want to turn back to fragmentA by using device's default backbutton, and i don't want to handle backButtonPress(). Just go back to previous fragment. My app does this.
Here is the question. When i press back button, my fragmentA calls the service again and it affects the user to wait. But how can i just turn to the previous fragment ?
I am looking for the best solution to turn previous fragment.

Categories

Resources