简体   繁体   中英

Getting the fragment from the current activity

Hey guys I have fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.recordings, container, false);
}

Here is my layout

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

How would I get this fragment object without adding it to my activity? Or add something, for instance like a button to fragment's layout from activity class?

An activity cannot reference a fragment that has not been instantiated somehow. You can instantiate a fragment via the fragment tag in xml, or create one programmatically in Java.

I suggest you take a close look at the documentation on creating (and referencing) fragments :)

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