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.
Related
I have a tabbed activity that shows a fragment by a viewpager, as usual.
This fragment have a list.
One of the actions of the user shows a dialogfragment to user insert a new item in this list.
I show the dialogfragment with edittexts to user create a new item.
The question is: how can I insert this item on the viewpagers' fragment list?
From any fragment I can call getActivity() to access the activity, but how access another fragment that is being shown behind the dialogfragment?
Thanks in advance.
Fragment with the List items - FragmentA
Dialog - NewItemDialogFragment
The method you're missing is setTargetFragment(). While building your NewItemDialogFragment, invoke this method passing the FragmentA as the target fragment for your dialog.
Later, you can access the FragmentA instance by calling getTargetFragment() inside NewItemDialogFragment and cast it to the FragmentA and add newly created item.
Alternatively, you can create the contract interface between the FragmentA and the NewItemDialogFragment
It sounds like you want to get the results from the dialogfragment (what the user has inserted on the dialogfragment edit-texts) and use this in the fragment that called the dialogfragment (to add as new item to the list) - in that case, the selected answer here solves this problem - also I think this Gist is a good resource to reference.
In your case, I also think implementing some sort of a custom listener/callback as they did in this Gist is a good idea. Hope this helps.
You can use event bus for this.
http://square.github.io/otto/
This is an example of usage:
Bus bus = new Bus();
bus.post(new AnswerAvailableEvent(42));
#Subscribe public void answerAvailable(AnswerAvailableEvent event) {
// TODO: React to the event somehow!
}
bus.register(this); // In order to receive events, a class instance needs to register with the bus.
Can I use onClick Listener for a button located in one of tab fragment, in Main Activity?
I think you want to perform some action in your MAIN ACTIVITY when a button is clicked in your fragment. If I am right, then just create a interface callback from your fragment to the activity and use the over-ridden method to perform whatever action you require.
Have a look at this
http://developer.android.com/training/basics/fragments/communicating.html
or this
onAttach callback from fragment to activity
Hope this helps.
Yes, you can think of a Fragment as a stand alone user interface that contains it's own animations and logic. So an onClickListener for a Button in a Fragment is no different than in an Activity.
Though the method using Interface is sufficient, this is the simple way to do it.
You can pass context("getActivity()") of MainActivity while calling
the fragment.
Create a method in MainActivity to change the fragment.
call the MainActivity method to set the Fragment.
((MainActivity) context).setFragment(fragment);
you are done.
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();
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.)