简体   繁体   中英

How to set an Activity's root view as fragment in Android?

I have a Fragment, and I want to set that whole fragment as root view of my activity. I have everything ready, and I'm instantiating my fragment programatically. I've tried (in my activity):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FeedFragment fragment = [...];
    setContentView(fragment.getView());
}

But I've got a null pointer exception. In other words, how can I make my fragment act like an activity? I only target ICS+, I don't need to support older versions, if it makes any difference.

Try this

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.all_lecturer_frag, container, false);

     ......

    return rootView;
   }
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
  setContentView(R.layout.xxx);
  //initializations...    
    if (savedInstanceState == null) {
        // During initial setup, plug in the fragment.
        YourFragment details = new YourFragment();
        getFragmentManager().beginTransaction().add(R.id.your_root_frame_layout, details).commit();
    }
}

A Fragment, by design, is intended to be a tool to help you reuse screen space and as such, fragments have to be present inside a container. So while a fragment cannot technically be a root view, you can have a fragment be the only view inside the Activity. For this, you should inflate the view for your fragment programmatically inside the onCreateView() method of the fragment. then you could have something like this in your activity's layout xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.package.fragment_name
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</FrameLayout>

And then, within your activity, all you have to do is:

setContentView(R.layout.main);

Since, the fragment is defined in the layout xml, it cannot be removed from the activity's layout (although the layout itself can be changed) and is tied to it.

Also, on a side note, notice that the root view is a FrameLayout and not the fragment itself. But in this manner, your fragment can be tied to the activity. But don't forget that the Fragment will still retain it's lifecycle separate from the activity's.

EDIT: If you need to create your fragment instance programmatically, you have to do:

getFragmentManager().beginTransaction().add(R.id.frame_layout, your_fragment).commit();

This is the only way to add your fragment programmatically. But also keep in mind that the Fragment's layout is not tied to the activity's layout. But you can use the Fragment's lifecycle to behave similarly as an Activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM