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.
Related
Activity A wants to update a listView inside the same activity.
Value is received from a surfaceview - scan, it should not update the listView until the finish button is pressed.
you can use fragments in your activity and then use fragment manager to navigate between your camera page and the listView page.
you can use bundles to share data between these two fragments.
use this to learn more about fragments:
https://developer.android.com/guide/components/fragments
and for sharing data between fragments visit this:
How to pass variables with Android fragmentmanager
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.
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
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.
I have two fragments like left panel one fragment and right side another fragment having.
The left panel fragment i having the add button from there when i click add button it launches another activity, from this activity i am trying the access the fragment but i am not getting.
This is the code i am using in my activity
LeftFragment left = (LeftFragment) getFragmentManager().findFragmentById(R.id.fragment1);
finish();
Can anyone help me.
Well, this because you can't get access to FragmentManager of other Activity, and it`s absolutely normal.
You can simply commit second fragment to the same container as a first one (use single Activity) and use method setCustomAnimations(...) for your transaction to animation.
Good luck!
you can define another ListFragment in your Fragment like this:
SecondListFragment SecondListFragment= (SecondListFragment )getFragmentManager().findFragmentById(R.id.second_list_fragment);
SecondListFragment.SetupSecondFragmentList();//its written on onListItemClick in FirstFragment
SetupSecondFragmentList() is a function that setup a list view in my Second ListFragment which has called from first one