Android: Clear editText data from multiple Fragment at one Click - android

I am using ScreenSlidePagerAdapter, ViewPager and PagerAdapter
I have three fragments and every fragment contains three to four editText fields.
Fragment 1: personalInfo
Fragment 2: contactInfo
Fragment 3: workInfo
WorkInfo also contain Clear Button to clear all editText data form all three Fragmnets.
When user enter value in personalInfo then contactInfo and at last go to WorkInfo.
Now at click of Clear button I want to clear all editText data.
I tried findViewById() but I could not get reference of editText from Fragment 1 and Fragment 2.
If anyone has a solution then help me.

Do something like this:
Make a boolean variable in your class initialized to false. When someone clicks on clear, change the value of that boolean to true. And in onstart() check this variable before showing data in edittext.

Use an interface to communicate between fragments. You could read more about this from the Android Developer Docs.
https://developer.android.com/training/basics/fragments/communicating.html
In resume, from your Fragment 3 you must call a method from your interface that must be implemetend by your activity. Then your activity must notify Fragment 1 and Fragment 2 to update their views.
Hope this help you.

Related

Passing multiple values from all fragments to its container activity by onClick event

How can I get multiple editText value from all fragments by onClick event which located in its container activity? Those EditText are being valid and checking by TextWatcher. I can't get any value of editText by implementing inflate and return fragment's layout to its contain activity.
What is more, how can I handle the situation of passing editTexts' value when all editTexts are valid if using Interface on fragment.
Thanks for any help.
There is several ways
Use SharedPreferences where you save all the value first and
then retrieve it later in activity.
Use interface class as bridge communication between fragment and activity
For Example:example interface class

Transaction between Fragment and Activity

I have a fragment and an activity.there are some fields and an button in fragment.When i click an button in fragment then i want to go to an activity.In that activity i have another button.when i click on that button i want to return from the present activity to the previous fragment.Every thing works fine.what i need is i want to recreate the fragment when i return to the fragment from the activity..i.e,for example in the fragment if i enter anything in the edit_text before passing to the activity, which is also presented when i returned from the activity.(the fragment is same as how i left from fragment) which is i don't want .i want a fresh page.i want to recreate a fragment when i returned from activity ..i need to select fields newly..can any one suggest any help..thanks in advance..
Try to use main FragmentActivity and Fragments and organize transactions between fragments. I didn't meet examples with Activity and Fragment in one Addlication.

Refreshing a fragment view from MainActivity

So I currently have an app that has 4 tabs (fragments). They are fragments A,B,C,D, in that order.
Fragment A is the first view opened (along with B because viewPager loads the view before and after the current view).
When I click a button in Fragment A, it sends Data back to MainActivity and then sends that data out to Fragments B and C.
However, this is where the issue comes into play. Since Fragment B was already called, the View isn't updated once I click the button and send the data over, but Fragment C is because the view wasn't called before.
Is there any way that I can remedy this?
You can do it a few ways right.
Just set the data to the fragment and have it update its views
Have all the fragments like B and C register themselves to recieve data from the MainActivity and when MainActivity gets it's data set you tell all the registered receivers of the new data
Recreate the fragment
Use an event bus and tell all subsribers of the new data and MainActivity, Fragment B would get notified of new data. Fragment C would get its data when created by MainActivity
I think this list is pretty endless tbh
The key here is the fragments need to fetch the data from the actvitiy aswell as be updated by the activity. In which case you need to break your UI update behaviour out of onCreateView and into its own updateUI() function. updateUI(MyData) can then be called from onCreateView and also called in a setMyData() on the fragment. Just make sure you check the isAdded flag in setMyData.
This pretty much says it all:
http://developer.android.com/training/basics/fragments/communicating.html
I used a simple fragment communicator that allows the activity to call the fragment, and the same for a fragment to talk to the activity.
You can change the views with the new data based on calling the method from within the activity. The way I do it is set the fragments in the activity then pass them into the page adapter this way I can call the methods within the fragment and implement the fragmentcommunicator interface on the fragments.
You can honestly even avoid the interface if you want, but if you are going to include the same method in all the fragments to talk to them it is easiest.
If you show code, I can show you a quick example.

how to save my fragment content? how to refresh my fragment?

I have 2 fragments which are called from the action bar of an activity. Both are gridviews, the first one displays applications with a dedicated adapter, and the second one displays a file list with another adapter. My problem is that when I launch a file then when I back to my activity I switch from one fragment to another, when I come back to the previous one, its content disappears. And when I rotate tablet I have the some problem, because my Fragment restart so for this I think that removing fragment give the possibility to create a new Fragment up to date. How can I save and reload data in my fragment.
How can I manage to update the content of the first fragment while coming back from the second one ? And how to remove fragment after the rotation in order to recreate the Action with new Fragment? I asked this questions but I don't have any responses. the code is given below
If your data is just strings or integers, you can make use of shared preferences to store and retrieve data.
Solution to your first problem -how to save fragment state
Use setRetainInstance(true) in you fragments onCreate() *it prevents your fragment from destroying and hence recreating.
Add your fragment to back stack
declare your adapter globally in fragment and resuse it when you get back.
when, you get back to fragment its onCreateView() method will be called directly. hence initialize your adapter in onCreate() method and use it in onCreateView().
Solution to your second problem -how to update fragment content
For this you can use interface. create interface in your second fragment and implement it in your first fragment. prefer this doc for this,
http://www.vogella.com/tutorials/AndroidFragments/article.html#fragments_activitycommunication

Activity/Fragment to Activity/Fragment to Activity/Fragment

Looking for a best practices solution to this issue.
Activity1 contains Fragment1 with a spinner of valid products
Activity1 contains an action bar menu option 'products' which launches Activity2 containing Fragment2 (list fragment).
From Fragment2 you can add/edit products using Activity3/Fragment3.
Changes made in Fragment3 I am getting back to Fragment2 with ActivityResult, and I know I could also achieve the same with an Interface.
The question is: What is the best way to get changes made in Fragment 3 reflected in the Fragment1 spinner?
I know I could reload the spinner values in Fragment1 in onResume() but if I didn't make any changes in Fragment3 this would be unnecessary.
I would use an Observer pattern. Implement an Interface with something like
public void onInputChanged(yourStuff);
let your first Fragment implement this Interface and Add the Fragment to a Listener Pool.
and from your Activity where the change happened, do something like
EventManager.fireOnInPutChanged(yourData)
EventManager would b the calls your listeners sign up to.
like this you wont need Activity forResult and you always know if something has changed.

Categories

Resources