Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: remove() Fragment--> add() new Fragment of same class again ->onCreateView and onActivityCreated not called?

I am destroying a programmatically created fragment with:

getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.test)).commit();

Which is determined in the xml file like this:

<LinearLayout
    android:id="@+id/test"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

</LinearLayout>

If I then create a fragment from the same class again in the mainactivity:

getSupportFragmentManager().beginTransaction()
            .add(R.id.result_bar, testinstance)
            .commit();

Then onCreate seem not called again (the fragment is just empty). What am I doing wrong here? Thanks.

like image 983
user2212461 Avatar asked Dec 18 '13 17:12

user2212461


People also ask

What are the differences between onCreate () onCreateView () and onActivityCreated () in fragments and what would they each be used for?

The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView() . Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.

What does the onCreateView () method?

onCreate() is called to do initial creation of the fragment. onCreateView() is called by Android once the Fragment should inflate a view. onViewCreated() is called after onCreateView() and ensures that the fragment's root view is non-null . Any view setup should happen here.

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

How do you adding removing and replacing fragments?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();


1 Answers

Explanation: Why FrameLayout for Fragment

What is FrameLayout:

According to Google Documentation on Commons Layouts and this answer of What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?, the ViewGroups as LinearLayout, RelativeLayout, AbsoluteLayout (decrepated), TableLayout, etc. are allow to display views:

  1. LinearLayout: "displays views one by one"
  2. RelativeLayout: "displays views regarding to another view"
  3. TableLayout: "displays views in a table"
    etc.

FrameLayout displays views by overlap the other. It is generally use to contain layouts:

"Frame layouts are one of the simplest and most efficient types of layouts used by Android developers to organize view controls. They are used less often than some other layouts, simply because they are generally used to display only one view, or views which overlap. The frame layout is often used as a container layout, as it generally only has a single child view (often another layout, used to organize more than one view)."

source: FrameLayout MobilTuts

"The Frame layout allows developers to display only a single or multiple UI elements within Frame Layout but each element will be positioned based on the top left of the screen, and elements that overlap will be displayed overlapping."

source: Android Frame Layout For Absolute Beginner

OK but, why do I need this for Fragment? (vs LinearLayout or <fragment>)

The Google Documentation of FrameLayout says:

"FrameLayout is designed to block out an area on the screen to display a single item."

FrameLayout will host a layout and it is willing for it. Whereas the rest of ViewGroups just display views. You can make a Fragment in all ViewGroups (I tested that, it was a surprise for me) but it will not a proper way to do this. The FrameLayout are:

"...the normal layout of choice when you want to overlap views."

If you create a layout with a <fragment .../>, your fragment will be not replace by another, because it is displayed, it is "attached" with its id on the view. To replace a fragment, you need to host it: "By encapsulating the Fragment within a FrameLayout, you can replace just the details" (see this answer).

Then, keep in mind that FrameLayouts are empties and can host a layout. Fragments (Fragment Documentation from Google, it explains very simply the facts to how use a fragment), when they are declared in xml, they must to have a class attached (an id), which cannot be replaced.
That's why we need a container to host this fragment and overlap the view of the activity!

Hope this will be helpful.

Note: If someone wants to edit this answer, because something it's not explain or badly explain, she/he could with heartily.

like image 147
Blo Avatar answered Nov 10 '22 09:11

Blo