Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which situation we want to add fragment without container?

Fragment transaction has method add(Fragment fragment, String tag), which does not place fragment to container, so it cannot have view. For what it can be used?

like image 729
Yarh Avatar asked Dec 14 '15 08:12

Yarh


People also ask

How do I create a fragment without a view container?

You can simply do it by accessing the view's provided by AppCompatActivity or Activity. Now, you can create a fragment, without adding a view container in your activity you can create as, Show activity on this post. You need to have a layout in your activity to contain the fragment (preferably a FrameLayout).

How to create and display a fragment in an activity?

You create fragments by extending Fragment class and You can insert a fragment into your activity layout by declaring the fragment in the activity's layout file, as a <fragment> element. Prior to fragment introduction, we had a limitation because we can show only a single activity on the screen at one given point in time.

How to create a new fragment without checking savedstateinstance?

Create a new Fragment without checking savedStateInstance In the Activity (or Fragment), if we have a Fragment as a view by default, then we can create it during onCreate as below. override fun onCreate (savedInstanceState: Bundle?) { There will be a problem with the above code.

What is the use of fragmenttransaction in Android?

Instead, a FragmentTransaction is used to instantiate a fragment and add it to the activity's layout. While your activity is running, you can make fragment transactions such as adding, removing, or replacing a fragment. In your FragmentActivity, you can get an instance of the FragmentManager, which can be used to create a FragmentTransaction.


3 Answers

From the Android Documentation:

However, a fragment is not required to be a part of the activity layout; you may also use a fragment without its own UI as an invisible worker for the activity.

How about this purpose ?

Simple example: an Activity starts an AsyncTask, but when device rotated activity restarts, causing AsyncTask to lose connection with the UI Thread. But this Activity can hold a Fragment (invisible, with no UI at all) that can handle all the AsyncTask work. When Activity recreated the Android OS takes care reattaching the Fragment, thus no data loss will occur.

like image 77
Evgeniy Mishustin Avatar answered Oct 13 '22 00:10

Evgeniy Mishustin


For Dialogs you don't have any container on normal app layer. It is directly added on Window with WindowManager(See WindowManager.LayoutParams for various types of layers).

DialogFragment has an API like DialogFragment.html#show(android.app.FragmentManager, java.lang.String) which corresponds to this.

like image 27
9re Avatar answered Oct 12 '22 23:10

9re


You can use fragments without UI (container) as a background worker (one benefit is that you can retain it during rotations etc) and for retaining data during rotations and other changes.

Reading http://developer.android.com/guide/components/fragments.html is strongly recommended.

Example of instance retaining: https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance.java

Also, here are similar questions (so this questions seems to be a duplicated but cannot be flagged due to bounty):

  • What is the use case for a Fragment with no UI?
  • Android non-UI Fragment usage
like image 28
GregoryK Avatar answered Oct 12 '22 22:10

GregoryK