I have one activity which contains buttons if I click any button according to that in my fragment layout changes should happen.
for this I created a interface it has one method public void onclick();
fragment implementing interface it got onclick method. I created reference to interface in activity I am calling this method from activity but not calling onclick in fragment plz help
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.
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 am trying to retain state of the multiple fragments in my project. Their parent is a single Activity and i need to implement the TaskFragment in all the Fragments but not the Main Activity.
In the worker-fragment example, an Activity is implementing the TaskCallback which works but how will it work with a fragment ?
https://github.com/alexjlockwood/worker-fragments
I am trying to implement TaskCallbacks in my fragments such that the interface's overriden method call goes directly to the fragment like this :-
public class MyFragment implements TaskFragment.TaskCallbacks{
//Some Code
//Code for initializing TaskFragment
//Overriden method of Task Callback
}
However, when i do this, it is showing an Ilegal state exception in onAttach method of TaskFragment that Activity is not implementing callback interface.
How do i get the instance of the fragment which is implementing this Task Callback ?