Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a fragment to an Activity without a container

Tags:

android

Is that possible I can add a fragment view on the activity view without specifying "fragment" view component in activity's layout xml file? Which function should I look for?

like image 855
weijia_yu Avatar asked Jan 09 '18 22:01

weijia_yu


People also ask

How do I attach a fragment to an activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

Can fragments be added without using a user interface?

The Android Developer guide has a decent section on the use of Fragments. One way to use Fragments is without a UI. There are a few references to using this as a means of background processing, but what advantages do Fragments bring to this area?

Can fragments be used without an activity?

After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity. While Using fragments in the project, the project structure will be good and we can handle it easily.

What method of the FragmentTransaction class is used to add a fragment to an activity?

It is the add() method on the FragmentTransaction which adds the fragment to the activity. The add() method takes two parameters. The first parameter is the id of the parent ViewGroup into which the fragment's View is to be inserted. The second parameter is an instance of the fragment to add.


2 Answers

Well, the UI of the fragment has to go somewhere. If you want the entire "content view" to be the fragment, add the fragment to android.R.id.content:

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getSupportFragmentManager().findFragmentById(android.R.id.content)==null) {
      getSupportFragmentManager().beginTransaction()
        .add(android.R.id.content, new ToDoRosterListFragment())
        .commit();
    }
  }

Otherwise, somewhere in the activity's view hierarchy, you need a container (usually a FrameLayout) in which to place the fragment's UI. Typically, we do that by putting the container in the layout resource.

like image 187
CommonsWare Avatar answered Sep 20 '22 01:09

CommonsWare


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        getSupportFragmentManager().beginTransaction()
            .add(android.R.id.content, MyFragment.newInstance())
            .commit();

   //Some of your code here
}

android.R.id.content is the container of entire app screen. It can be used with Fragment:

The android.R.id.content ID value indicates the ViewGroup of the entire content area of an Activity.

The code above will insert the View created by Fragment into the ViewGroup identified by android.R.id.content.

like image 30
Faxriddin Abdullayev Avatar answered Sep 20 '22 01:09

Faxriddin Abdullayev