Refresh the fragment when user change settings - android

I have a main activity. From this activity user can navigate to profile and in profile user can edit settings.
What I want is: when the user makes changes, I want to refresh the fragment in main activity according to these changes. As I can only update the fragment that main activity is showing when that activity came to top again. So how can I tell main activity that data has change and update the UI accordingly.
So I was wondering what's is the best way to handle that kind of scenario.

Consider creating a custom BroadcastReceiver inside the Fragment class, and on any change of settings send Intent with your custom Action.

If profile is an activity, start it using startActivityforResult, and get new data in onActivityResult if fragment instance isn't null , call some method say update in that fragment. If fragment instance is null, create new one using new data.
If profile is another fragment in the main activity, then store the changes in SharedPreferenceManager. In On Resume of your MainActivity's FirstFragment fetch data from SharedPreferenceManager. If it isn't same redraw that fragment else leave it.

Related

Save data filled in multiple fragments together

Reqirement-I have an activity with three fragments. Each fragment has a form for user to fill and a proceed button.Proceed button will move user to next fragment.On third fragment proceed button will send data filled in the form of all three fragments to server.
Approach- I declared an object in activity.In each fragment when user click on proceed I assign fields of that object with data filled in fragment.When user click on proceed button in third fragment that object is submitted to server.
Is this approach correct? Is there any better way of doing this?
You could use Shared Preferences instead if you don't want to maintain an object and want to retain the information the user entered for later use in the app.
Working on main activity thur open fragment.
Create home activity one static method. and call after one fragment button clicked.
i have attach below step Please check.
1.Home activity create one method.
2.method name create addUserLineInfoFragment
3."A fragment" clicked set this code
((YourActivityName)getActivity()).addUserLineInfoFragment();
4.Home activity this method "addUserLineInfoFragment" set "B fragment" open code

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

Restore, or bring fragment instance to front

I have a single ActionbarActivity with two fragments. I have a menu option (maintained in MainActivity) to open a camera intent and capture a picture. When I return from this event, I want to update the first fragment.
Currently, if I select this menu option while the first fragment is visible, I can update the UI by using a reference to an instance in the MainActivty's onActivityResult(). However, if the second fragment is currently visible, the app crashes. (I am away from Android Studio but will be able to post error message shortly, if necessary.)
How can I - from the MainActivity's onActivtyResult() or elsewhere - ensure that the saved instance of the first fragment is returned to and updated, regardless of which fragment is currently visible when the option was selected? Or is it just as easy to create a new instance of the first fragment? (It would be created with the updated information automatically.)
you can get existing fragments through the FragmentManager from the Activity.
see here how to do this.

How to create notification from fragment and navigate to other fragment on notificationclick

in my application I am having 1 activity and 5 fragments. I want to create a notification on button click in the first fragment(Homefragment). I dont have an idea about creating notification from a fragemnt. Then I want to navigate to ActionFragment when I tap on the notification. How can I do this?
You can read up on communicating between fragments here http://developer.android.com/training/basics/fragments/communicating.html
I've made a brief summary of how it would work:
What you would need is a interface that is declared in the first fragment and for the activity to implement it, and pass it to the fragment when it is created.
This way, the fragment can call the interface (mListener.notify()) which will then call the activity. The activity will then replace the current fragment with the new one you wish to open.

Check for calling activity or call setResult anyway

I have two Activities. One extends Fragment activity and displays some data in a list (fragment1) and details if one clicks on a list item (fragment2). Fragment2 has a menu item that can be clicked to edit that data set. If the user wants to edit the data fragment2 calls the other activity per startActivityForResult in which he can edit the data set.
Should I check in the edit activity if it was invoked for a result by getCallingActivity or should I setResult anyway? (I finish() the activity after saving to restart the activity in order to clear the EditText views, since one can edit or insert new data sets in that activity.)
You can go ahead and just call setResult() without checking the calling Activity. The Activity that doesn't care about the result will simply ignore it with no harm done.

Categories

Resources