Only thing that i want is to detect button click in fragments .
There are many ways to interact between fragments and activities, and you can find them by searching here on SO, but the easiest way I've found so far and the one I love using the most is a 3rd party library, called EventBus.
You can simply post an event on button click, custom event which can carry all kinds of data, and then in your fragment, simply write a method that listens for that event and does whatever is necessary.
If your button is interacting with your fragment, it should not be separated.
However, you can do:
getActivity().findViewById(...)
inside your fragment
Related
The idea of using MVVM is that view observes ViewModelchanges and acts. I'm using an Activity which uses 7 Fragments and the navigation between them goes through observing individual changes in the Activity from different Fragments and launching/replacing Fragments accordingly. For instance,Fragment L calls setValue, then as a result the main Activity receives the event and switches to Fragment M and Fragment M calls getValue from the observed MutableLiveData and not directly functioning as a listener to changes. Does that the right structure or should each Fragment observe changes by himself ? What would the right way to handle multiple navigations between multiple Fragments
I don't see anything wrong in what you just said. Fragments should be independent from each other unless they are nested.
If you need communication between fragments that can be done in many ways. The most basic way is to do that through activity. Activity knows how its fragments interact with each other, but fragments remain independent. They can listen to events, no problem here, but make sure you have a 'rule' in your project about how you handle events.
E.g. I prefer having one listener at a time to have the order more predictable. If I want multiple fragments to handle the event, I usually place the handler in the activity and then pass the event to fragments in the exact order I need. Otherwise it might quickly get out of control.
There are other ways too, such as EventBus, BroadcastReceivers or any other event-based mechanism.
I hope that helps. If I didn't answer your question then to answer it more precisely I'd need the question to be more specific.
Currently we are have a container activity that hold 2 buttons(next and cancel) along with a content layout that switches depending on the fragment. The fragments follow the mvp pattern but the omain questions seems to how to correctly implement the Next and Cancel button on click events.
Would it be better to make a Presenter for the MainActivity and pass that through to the Fragment and have the fragment work with that for the two button events? Or should the Fragment create new onClicks for the button on each fragment change? My way of thinking seems to go along the lines, the two buttons can be considered part of the fragment view at that moment in time, so the Fragment should be concerned with handling them. But wouldnt this lead to more code writing? Any help would be appreciated.
In your case my choice will be a simple way.
Activity register as listener onto fragment. Fragment handle the button event and call to activity. Each acitivty deal with event action.
As in your description fragment is a plain unit just to introduce next & cancle functions. Implements MVP on this unit cause more code but no income.
Now the question is on the activity side. If there is full business procress then go MVP way, if not just add two functions to respond button is good enought.
There always exchange, MVP more code & complex relationship for extention & team work, plain function call less code & simple procress for small unit & extention unfriendly.
I would like to use tabs in my Android application and since TabActivity is deprecated, I want to do it the Fragment way.
I already constructed the skeleton of the app, and I have 3 tabs, and each the tab has its own seperate Fragment.
My questions are:
I ended up having 1 Activity for the whole app and a lot of fragments. Is this normal?
In the fragments I have buttons. Each onClick method of these buttons expect to has its handler in the Activity. I've found this post: Handling onClick events with fragments. Seriously?? The Activity will catch all the onclick's events from all the fragments and call the appropriate method of the relevant fragment?? Can't I handle the onclick event of a button that located inside a fragment in the fragment's code?? If I can't, Google should get some spanks!
Thanks in advance!
The answer to question number 2 is to implement onClick in the code of the Fragment... No way of doing this in the XML?
I've recently decided to update my app to support the new fragments feature in honeycomb 3.0.
My Application currently works on a list view that opens different activities depending on which list item is clicked.
Using an adaptation of the code in this tutorial I have created an app that consists of only two activities, but depending on which list item is clicked the second "viewer" activity launches using a different layout xml.
Unfortunately I haven't been able to figure out how to call the old methods that had all the functionality. Should I Import all of my old activities and then call the methods into the viewer activity (I may need some advice on how exactly to do this) or should I just put all the methods directly into the same viewer activity (please consider the size of these methods(which is very large by the way)).
Once everything is working with two activities upfront then it will be a pretty simple task of "fragmenting" the app as demonstrated here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
Thanks
As James has pointed out you will have to move the business logic from your Activities to your Fragments.
To handle events you can create a listener Interface. The CONTAINER activity/ies will implement this interface. As fragments has access to the container activity you will be able to delegate to the container Activity the "logic" for the desired events. For this events the activity will decide whether to launch a new activity, show/hide new fragments or whatever.
I had a similar question, take a look to the question and answer: here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
I think its possible to allow multiple fragments to occupy the same space in an activity. Again, take a look to the answer here ... I think the concept/scope of Activity has change a bit and now an Activity can contain different Fragments which every one will allow user to do a single focused thing.
I'm not sure what you mean by "call the old methods that had all the functionality". You'll want to rewrite all of your activity classes as fragments. Check out this tutorial here (it's very concise). Basically, you'll want an activity that consists of a ListFragment and a FrameLayout. Your ListFragment will update the FrameLayout by changing to the appropriate Fragment based on which row was selected.
I've got an Activity which displays a list of custom components (in a LinearLayout). Each component represents a user in the database, and contains a button (among other things). When I click the button in the custom component, I want to pass a message back to the Activity to save the the user. The button knows the id of the user.
I was wondering how best to communicate back to the activity? In the click event of the button, should dispatch a new event (and catch it in the activity)? If so, can I add the user ID into the event? I'm new to events but I think I need to create my own custom event type perhaps?
The other way I thought of was to pass a reference for the activity into the component, so that the button can just call the method on the activity e.g 'component.parentActivity=this' but although I think it would work, I'm not sure if it's the 'proper' way to do it.
Thanks
You can implement the View.OnClickListener interface. The onClick method you'll need to implement will have the View (the thing that was clicked) passed to it. So you should have all the details about what was clicked and thus handle it the way you want from there.
What I'm not clear on in your question is what you mean by 'component'? Is that a custom View? Or a layout of Views? Or other?