I have two fragments in app. When user launch the app, in main layout fragment container shows first fragment, when user click on the button, second fragment replaces first fragment.
I want to do this: when app launch, in second fragment loading some text from the GET url request. And when user click on button to show seecond fragment, all text has already been loaded.
How can I do this ?
Maybe, make function "loadContent" in second fragment and when app launches call this function..
Is anybody have any ideas/info about this, please provide me^)
Make the GET call in Activity's onCreate method. Define your own interface which has an abstract getter. Implement the interface on Activity, and override the getter to return GET response.
In fragment onAttach(Activity) initialize interface like :
Interface callback = new Interface(activity);
now get the data, anywhere in fragment lifecycle after onAttach, from callback object like :
callback.getterFunction();
Related
I have an activity like this:
There are 2 buttons A and B on toolbar and a frame for fragment to take over. Say I have a string variable named button_type in fragment.
I want to have a system so that when I click button A in activity, the button_type in fragment sets to A and when I click button B in activity, the button_type in fragment sets to B.
How to do this?
Please note that I may click the buttons (A,B) when the fragment is already active (its not like after I click one button, the fragment comes.)
Thanks in advance.
Edit:
Currently I am trying this:
In MainActivity I get similar string button_type and set it as A or B according as button click and use the method:
public String getData(){return button_type;}
And in fragment I use: button_type= activity.getData(); in onViewCreated.
But it only seems to have the initial set value of A and B (which is A) and does not change when another button is clicked.
I think the best way if you use an interface for managing text in the fragment. the fragment will implement the interface. When the button click call the function in the fragment which is implemented by fragment.
Another way you can create an object of the fragment using findFragmentByTag() or findFragmentById() and then call the function in the fragment which handles the text.
Create an interface with methods onClickA(String buttonType) and onClickB(String buttonType).
Then create an object that implements this interface (or make fragment implement this interface by itself). I'll call this object listener.
call setButtonType(String buttonType) in listener methods implementation.
Then pass your listener to activity (you can get an instance of parent activity in fragment with getActivity() and cast it to your activity class) and in onClickListener of button A (in activity) call listener.onClickA(yourString) and do the same thing for button B.
Concise: I am looking for a way to update a view in an activity from a fragment that the activity contains.
In detail: I have a fragment and an activity that contains the fragment. The activity has a navigation drawer and the navigation drawer contains a image view. What I am trying is to update the image view in the navigation drawer when a HTTP GET request returns a response from the fragment; the response contains a URL to where an image loader parses an image for the image view in the navigation drawer.
Given this, I am trying to get an instance of the view in the activity from the fragment, but I am not sure how to do so. Even, I am not sure if I am on the right direction...
I will greatly appreciate any input.
Regards,
Create Interface:
public interface mInterface{
public void updateIMG(String url);
}
Implement this interface inside your activity and Override updateIMG() method.
Inside your fragment what ever you need to call updateIMG() just initiate interface and call a method.
mInterface listener = (mInterface)getActivity();
listener.updateIMG(url);
Then you call this interface method it will run code inside overrided method inside activity.
In case you get Only the original thread that created a view hierarchy can touch its views., try.
Declare view as public which you want to update and then you can access that view from the fragment and can update from there
You can use a LocalBroadCast to send a broadcast from Fragment whenever needed and then receive the broadcast in Activity and do the changes.
You can also implement Activity, Fragment communication through an interface. Refer this link to implement the same.
Following the MvvmCross sample here
I am trying to show an activity, that contains a fragment. When I call ShowViewModel on the Activity, I see the activity, without the contained fragment.
If I call ShowViewModel on the Fragment, the Activity AND the Fragment are created.
Does this mean, in order to show a fragment I need to call ShowViewModel on the fragment and not on the parent activity? surly I should be able to call ShowViewModel on the activity and have that create the fragment.
Sorry if i'm missing something here.
Thanks
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 4 tabs in an Activity.
Each of them is a Fragment. And every Fragment has a ListView.
So, if i change the ListView in Fragment, it must change the ListView in all other Fragments ie.., Tabs.
The problem i face is while creating the interface instance.
It takes it's own onClick() method.
In case i want a callback to the parent activity i could have done that by overriding onAttach. But how to make a callback to a Fragment?
From Developers site:
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
So, make a callback to the Activity which in turn makes a callback to other fragments??
Thank You
It's pretty simple,all You need is steps below:
1) From onClick method in your first fragment make a function call of activity:
((IYourActivityInterface) getActivty()).activityMethod();
2) In your activity find fragment by tag or id and run it's method:
public void activityMethod(){
Fragment tabFragment = getFragmentManager().findFragmentByTag("second_fragment");
// or Fragment tabFragment = getFragmentManager().findFragmentById(R.id.frag);
if (tabFragment!=null){
((IFragmentInterface) tabFragment).fragmentMethod();
}
}
Hope this is what you are looking for.)