I'm migrating an activity to a fragment. The fragment will ultimately be placed in a tab page.
I have copied the "grouped list" from the Conference example. The listview was on an activity. I am now moving same to a fragment. The method OnViewModelSet() does not exist in the view. Where am I supposed to moved the code contained in OnViewModelSet() when using a fragment?
Here is a very good talk on this topic by Corey Latislaw. She gave it at DroidCon London 2012.
Extends your class from fragment instead of activity and call fragment class functions with the view.
I suggest you to please upload some piece of code.
Related
I want to separate logic fragment from activity but the problem is I make api call and save data in fragment. And when user click a item in fragment. I need to send parcelable data to other fragment to show detail info about item.
Is launching fragment in fragment anti pattern for android ?
I would like to hear some opinion about this matter.
Yes, is totally an anti-pattern, remember that you need to see the Activity as a container and fragments as independent sub-screens, so is the Activity responsibility to manage the fragments. I.e.: If you have a Post activity you can have a PostText fragment, a PostImage fragment and all of that is manage by the activity, every fragment is attached to an Activity.
It is not a common practice to have a nested fragment inside a fragment even it can be done. However, it would be better to have an activity as the centric container for all your fragments. You can use EventBus (GreenRobot / Otto) to separate the concerns and do all the API calls in another class and send the results by subscribing to this event.
I'm really sorry, but atm I can't test any code. But I have a question. How should we work with fragment manager? I got an error when I was trying to add fragmentA, then fragmentB, and then again A. I got an error : fragment is already added. And here is the question: should I call transaction.Add at launch and then replace it with others fragments or I shouldn't.
Thanks for attention and sorry for my English, it is not my native language
You can add it if you want but you on first launch you can actually just use .replace even if there isn't a fragment currently loading into the frame and it will still work.
I need assistance problem solving. Not necessarily looking for code, though that wouldn't be rejected!
I have a central FragmentActivity housing a FrameLayout that I use to swap Fragments as the user navigates around the core of the app.
On my ActionBar is a search widget. Typing in a search query opens up a ListActivity of List Options that matched the query. So this is essentially opening up an Activity over my FragmentActivity. I like that idea as this is "side detour" in navigation.
My problem I need to solve is this: When the user selects List Item after search, I want to somehow close Activity, and replace the FrameLayout in the Fragment Activity "underneath" with the information that they searched for.
I tried to make the ListActivity a ListFragment but couldn't work that out. Unless making it a FragmentActivity with an enclosing Fragment is the answer. - Any other ideas?
Try starting the ListActivity with startActivityForResult() method, and in your FragmentActivity override the onActivityResult() method, and make all the magic there.
Could you implement a callback method in the Activity so that the FragmentActivity could respond to whatever the Activity wished to convey?
In my existing app I am porting two activities to fragments. The case is the classic dual panel mode with a list on the left and the content on the right.
The doc says that I should avoid to manipulate fragments within fragments, passing instead through the host activity. Said that I am using callbacks to the activity.
The first doubt (maybe banal) I have is:
How to avoid to duplicate the same code in the activity that hosts the
2 fragments and into the activity that wraps the fragment when not in
dual mode?
I'll try to explain. So I have:
ListFragment and ListFragmentActivity
ContentFragment and ContentFragmentActivity
because both fragments can live independently from each other, then:
HostActivity
that implements a listener invoked from ListFragment for adding/replacing the ContentFragment
My question is: when ListFragment is instead hosted from ListFragmentActivity, how to avoid to duplicate the code present in the HostActivity into ListFragmentActivity.
Guess I am missing something, thanks in advance.
Get rid of ListFragmentActivity. Have HostActivity handle the case where there is either one or both fragments. Then, by definition, there is no code duplication. See: https://github.com/commonsguy/cw-omnibus/tree/master/LargeScreen/EU4You
I want to have a FragmentActivity with 2 Fragments. One of them runs a DroidGap, and the other one runs a permanent camera. Is there a way for me to do this? This diagram below illustrates what I want:
FragmentActivity (
Fragment 1: camera
Fragment 2: DroidGap
)
DroidGap extends Activity.
How do I run an existing Activity as a fragment? I tried startActivity() from Fragment.onActivityCreated(), but that simply calls the parent FragmentActivity.onActivityCreated(), which switches the entire Activity to the DroidGap. Is there an adapter that would treat an general Activity as a Fragment?
http://developer.android.com/reference/android/app/Fragment.html
PhoneGap v1.9 comes with CordovaWebView. This allows you to add PhoneGap into Fragments or ActivityGroup.
See Wiki at: http://wiki.apache.org/cordova/CordovaWebView
Working example: https://github.com/infil00p/CordovaActionView