It might be an easy question but I have been working on it for hours and couldn't solve it. Sorry if it was asked before.
In my activity I have 2 Fragments. In one fragment I am getting JSON from server and putting in ArrayList. After putting in ArrayList I have to add another fragment passing first value in ArrayList.
Here is the problem, I want to add Fragment when my ArrayList is completed, after it got all values from server. I am making service call in my onActivityCreated() method, server call is happening in another class and I am getting Bundle in a method called onRequestFinished() and I am putting JSON to ArrayList in this Overridden method.
P.S. I tried to put AsyncTask but I couldn't make it since I have to wait the response from onRequestFinished() method.
How can I handle it and complete Fragment Transaction after filling my ArrayList?
Thanks
I suggest using startActivityForResult in the first fragment. When the parent activity receives the onActivityResult it can then start the second fragment. There are plenty of examples of how to do this on SO and elsewhere.
Even if you do not want to start an activity, you can use the same mechanism to communicate back to the activity when your first fragment has completed e.g.:
Is there a method that works like start fragment for result?
Alternatively you could of course just implement a callback mechanism from your first fragment - but personally, I like the onActivityResult approach.
Related
I have another one question about transfering data from Activity to Fragment.
In my activity I have next situation: one part of UI is situated in Activity, and another (more dynamic part) is situated in Fragment.
The data which I need to populate my UI elements in Activity and Fragment is on server. To get that data, I am sending request to
server in Acvitity's onCreate() method. In Activity's callback method: void onDataLoaded(List<MyObject> dataList) I get data from server.
And in this method I am creating my Fragment and setup data to it. I am passing data to it through the Bundle object duding creation. Everything
is ok with this. But the issue is in next: on network reconnect I need to load data from server to be sure that all data is up to date. And of course I
need to reinitialize data in Activity and Fragment. But I don't want to fully RE-CREATE fragment. I want just to setup new data to it's fields.
How can I do that properly? Is it a good way to to keep reference in my Activity to that Fragment and call some public method: myFragment.SetMyCustomData(List<MyObject> dataList) ?
I understand that the best way to load data from server in my fragments onCreate() method, but I can't split it into two API calls and I need that data in Activity as well.
Thanks.
Well this is not a transfering problem . Anyway you can use Event Bus for easier communication from Activity to Fragment and vice-versa. It is very easy to use . And as for your problem I belive you are not doing what you need in the right time .
https://github.com/greenrobot/EventBus
I made several api calls from a model class of an Activity and upon receiving responses from each of them, I need to feed the data to a Fragment one after the other by invoking multiple instances of the same fragment.
Ideally, the next fragment will only be fed with data after the previous Fragment has exited (by response from a listener).
I have looked everywhere and couldn't find a solution to this problem. I have tried using AsyncTask with a CountDownLatch to block the next api response before getting an action response from the initial Fragment but after it only invoked one Fragment (I know how many fragments I should be creating) and back to normal Activity view.
Any thoughts on how to deal with this problem?
There are 2 ways for this
Create a constructor to the fragment where you pass the data when you create instances.
mFragment1 = new xFragment(dataA);
mFragment2 = new xFragment(dataB)
To use Interface and Implementation to pass data from Activity to. Check the link below
https://developer.android.com/training/basics/fragments/communicating.html
I use Navigation Tabs in my app. I have 3 fragments which load different data from Internet. I want to know the best place where to put the code that make the HTTP request, in onCreate, onCreateView or onActivityCreated?
Usually, I put all the code (requesting data, populating adapter, inflating view...) in onCreateView. I also saw many people doing it on Internet.
But this guide https://github.com/thecodepath/android_guides/wiki/Creating-and-Using-Fragments do things differently. So I want to be sure on what to do exactly.
I would normally put the code to refresh the view with new states in the onResume(). I would only inflate the view in onCreateView and possibly set adapters and such.
It also depends on how fresh you want the data. I you only need to load it when the user starts the app, I would load it in the onCreate of the Activity and then load all data for the fragments in one batch. You can then store the data and retrieve it in the different fragments.
Also you always want to load data from the internet on a different thread. If you are set on loading the data in the fragment itself, I would start an asynctask in the onCreate and refresh the view for the fragment in the callbacks from the asyntask. In the onCreateView you can put default values, or let the user know the data is comming via text or some other notification.
If you want real fresh data you can start the asynctask in onResume() of the fragment.
Answering really late, but if you are making HTTP Requests, I would suggest putting them in onActivityCreated(). onCreateView is great for initialising and binding your views. onActivityCreated is called once this is done and onCreateView returns, as you can see in the answers to this SO question.
You can read more about this here and here on SO.
I have a scenario and I an not sure on what path to go.
Scenario
The app has a Home activity which displays various fragments. The data in the fragments can come either from the web or a local database and is retrieved using an asynctask.
From what I saw, I have 2 alternatives:
Put the Asynctask in parent activity and then use fragment.newInstance(parameters) to pass the result to the fragment. However, if in my asynctask I need to update the progress or some info on the fragment, each time I will have to call newInstance with the new set of parameters.
Add the fragment and put the asynctask in it, in this way when progress is needed, I can update the fragment's views, as I have access to them + when the asynctask is done, I can populate the list with the info.
What would be the correct approach ?
LE: actually for point 1 in order to update the fragment I can call fragment's public methods after I find it with findFragmentById in the parent activity
A better way if you have multiple tasks would be to use an IntentService :
http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/
You would have a better control to what you're requesting and what you want to cancel.
I would go with the second approach.
My primary reason though would be to avoid the issues that can happen on screen orientation change while the AsyncTask is working.
I would go with method 2, but take it a step further.
Have a separate fragment to run your async task. This way, you can handle any configuration changes (not just rotating screen) without any issues.
In another fragment, you can display the data. You can pass the data from your async task fragment via callbacks to the activity, and have the activity call a method in the display fragment to update the data.
I cannot for the life of me figure out how to update a Fragment/entire FragmentManager. I'm using the FragmentManager that comes with the SDK (sliding tabs) and inside one is a form for a login. When the login is finished (which I have figured out, it uses an AsyncTask) I'd like to reupdate the entire FragmentManager section. FragmentTransactions have failed me (or at least I couldn't get them to work) so I'm wondering what's the best way to just refresh the UI.
Also, I have the custom menu with a logout button. When the user logs out, it should also refresh the UI (the tabs change based on whether or not the user is logged in).
TL;DR How do I refresh/reload an entire fragment system from inside and outside of the Fragments?
Also another thing that would be helpful, how do I make a Fragment update with new information (passing an array or a JSON array) after a different AsyncTask completes?
You would have to paste some of your code to see what is wrong with the FragmentManager. Transactions should be completed after you call <FragmentManager>.commit().
In regards to your second question:
To get your Fragment to update with new information, you should be able to just add a method like normal to your Fragment and call it. If you are calling the method from the AsyncTask then just be careful with Context. Take a look at this:
How to call parent activity function from ASyncTask?