So I have re-write it.
The problems are:
If a fragment is declared in xml, then you can't call replace on it. Why?
If you want to put a fragment into a FrameLayout(id, frame_layout), then call
add(R.id.frame_layout, fragment) will result in "No View exist Error". There is a way around this by calling add(android.R.id.content, fragment).
The problem is, what if the R.id.frame_layout isn't the base layout for your activity?
Also, in dynamic fragment dispatch(using replace and add), maybe only one container could contain one fragment rather than two?
I have browsed a lot...
Q1. If a fragment is declared in xml, then you can't call replace on it. Why?
Because that's a static fragment. Android system would always stick to it. There is no way to remove or replace it. Any new fragment that's "add" or "replace" on the same id would be placed on top of each other.
Q2 If you want to put a fragment into a FrameLayout(id, frame_layout), then call add(R.id.frame_layout, fragment) will result in "No View exist Error". There is a way around this by calling add(android.R.id.content, fragment). The problem is, what if the R.id.frame_layout isn't the base layout for your activity?
This is not true. Depending on situations. Generally speaking, the id in the function call "add(id)" only means the container of the fragment or the view which is to be replaced by fragment. android.R.id.content represents the buttom layer of the views in the activity.
Also, in dynamic fragment dispatch(using replace and add), maybe only one container could contain one fragment rather than two?
Well, it depends. Just for the sake of clarity(if you want co-workers to understand your code), it's good habbit to make sure that only one container contains one fragment. That's dynamic fragment, not the rule of static fragment.
Related
I'm struggling to figure out how to create fragments that have their own layout files and take up the whole screen, as opposed to adding them to the activity's layout.
For instance, in my activity there is a button which should call a RecyclerView Fragment that takes up the whole screen, let the user pick an item, and then return to the activity. All the examples I'm finding though use transactions to add or replace on the activity's layout. How do I make fragments that are inflated from their own layout files and call them from the activity?
And sorry, I'm sure there's a better way to ask but I'm just going through docs and vids trying to learn.
A few line difference between Fragment and Activity:
An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: http://developer.android.com/guide/components/activities.html
Whereas a Fragment represents a behavior or a portion of user interface in an Activity. http://developer.android.com/guide/components/fragments.html
Two days ago i asked the following question:
Change the fragment in a framelayout from within another fragment of said framelayout
A fellow user Krish, really helped me out in finding out what was wrong with the way i thought, but i am stil not sure how to actually get what i want done.
I want to be able to switch between three fragments in one FrameLayout.
- the first of the three is loaded at the start of the parent fragment and when back is pressed at the second fragment
- the second must be replacing the first at the click of an item in the listview of the first fragment, and when the back button is pressed from the third fragment
-the third must be loaded when a button is pressed in the second layout
I've tried achieving this by calling the following line whenever the fragment must be changed. A1_frame is the FrameLayout of the parent Fragment/Layout and A1_B0_C2 is the fragment i am replacing
getChildFragmentManager().beginTransaction().replace(R.id.A1_Frame, new A1_B0_C2()).addToBackStack(null).commit();
From what i understand the problem with my solution is that it isn't possible to replace a fragment in the FrameLayout of a parent Fragment/Layout, but if it would work, it would solve my solution. thats why i chose to put it in here.
I hope someone is able to tell me what would work!
getChildFragmentManager() returns the Fragment manager of the Fragment it's being called from, in this case whatever Fragment is in A1_Frame
The method you're looking for is getFragmentManager(), which returns the Fragment Manager of the Activity/Fragment that the Fragment is a part of. I.e. MainActivity, or whatever is creating your first fragment.
it is me again and i first wanted to apologize for asking that much today.
Here is my situation.
Because of my design my FragmentActivity( topmost parent in obiect hierarchy) has only one member, which for itself has a collection of objects like fragments, i have one level between fragmentactivity and the fragments. I would like to avoid some kind of event bus and/or observable pattern to pass the events of the fragments buttons to my object which would forward them to fragment activity to be then handled there.
Especially showing fragment, deleting replacing fragments is preferred to be handled in my object in between or even in the fragment itself.
Is this recommended? Or will I get into any trouble?
I'm implementing a fragment hierarchy similar to the one described in Fragments (Android Developers).
In adition in tablets in portrait the app should behave as in the second case. My problem is to handle the transaction from an orientation to other.
The first idea I considered was:
From landscape-to-portrait:When the activity A goes to portrait: Remove the fragment B to the view. Start activity B for result passing the propper values for recover the original fragment B state
From portrait-to-landscape: When the activity B goes to portrait. finishes (with the fragment 2 status in the result). The activity A with the result restores and adds the fragment B to its layout.
But this solution is pretty complex and I think it provably is not a nice idea. The alternative solution I have considered is only to have an Activity. That activity layout is:
<FrameLayout>
<LinearLayout>
<Fragment A>
<Fragment B>
<Slot>
For small devices:
The app removes the fragment B and when a item is selected add to the backstack the fragment to the "Slot"
For tablets:
Using fragmentTransactions the fragment B is moved from Its position to the "Slot" using the backstack to behave properly to the orientation changes
I think the second option sounds better but, is the correct way of doing this?
Thanks
If you want my advice, I'd say it depends on way too much factors. I think you should stick with what you find manageable enough. It depends too on how complex your app's screen flow is.
Keeping it in one activity is, for me, a good idea if you don't have that much fragments to manage. An advantage of this approach is that you don't need to fiddle around with the life cycle of two different activities.
Anyway, finding the implementation complex is in a way an indicator that what you're planning won't be manageable for you in the future.
Hope that helps!
I dont get why would you want to do it in such a complicated way. Have one activity on tablets, two activites on phone. Have first activity implement a listener that would fire if list fragment's item was clicked. The activity knows if it is inside single or dual pane mode, so inside that onItemSelected callback method, have it either start a new activity in case of a single pane mode, or replace a fragment, in case of a tablet.
You can also see this, using Master/Detail template when creating a new project.
I read quite some articles about fragments, but I am still confused about how to do what.
I have a MainActivity, which displays two fragments side by side. In one of the fragments I have a button and defined in the fragments layout XML for the button
android:onClick="buttonClicked"
Now I want to implement that method
public void buttonClicked(View view)
I would have assumed that this has to be implemented in FragmentA.java and not in MainActivity.java. But it only works if that method is implemented in MainActivity.java. Why is that? To me that doesn't make sense. Pre Honeycomb a method belonging to one activity stayed in that activity, now on a tablet I am merging many activities to one MainActivity and all the different methods are merged? Whatever do you put for example in FragmentA.java then? What if you have to start you an own activity because this app runs on a handheld, then the onClick method has not to be in the MainActivity but in the Activity which needs to be called then. I am pretty confused at the moment...
I'm not sure what the specific problem is, but maybe this will help.
From the Android documentation on Fragments:
You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.
That is, you should never manipulate a fragment from another fragment; rather, this should be done through the underlying Activity. Read the "Creating event callbacks to the activity" section in this article for more information (it's important stuff!!).
On the other hand, if you want the button to perform an action within the Fragment itself (i.e. if you wanted a Button click to change the text of a TextView within the Fragment), you should implement this in the Fragment, not the Activity (this is because the resulting behavior is contained within the Fragment and has nothing to do with the parent Activity).
Leave a comment and I can clarify if my post is confusing... I only recently began to understand Fragment's myself :).
Well,
I guess it is related to hierarchy of android context structure.
Activity is host of all child views and hence you can say fragment is actually using its host's context.And that's why when you use onClick with fragment system always searches it in Host activity of fragment.
Check it on.
Android developer onClick attribute description
I haven't checked one thing but you could put a test.
By providing implementation in host activity rather than in fragment,but use onClick on layout file of fragment.It should call parent's method.