简体   繁体   中英

Default fragment on Activity Launch

I'm trying to have a fragment loaded as the default view for the activity, but I just get a blank screen and huge memory leaks.

Activity file onCreate method:

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

    setContentView(R.layout.activity_login);

    if (savedInstanceState == null) {
        FragmentManager fragmentManager = getFragmentManager();
        int fragmentTransaction = fragmentManager.beginTransaction()
                .add(R.id.login_screen_fragment, new FragmentLogin())
                .commit();
    }
}

The XML for the activity:

<FrameLayout 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"
    tools:context=".LoginActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/login_screen_fragment"
        class="com.test.project.FragmentLogin"
    />

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/forgotten_password_fragment"
        class="com.test.project.FragmentForgottenPassword"
    />
</FrameLayout>

And the Fragment class (relevant parts):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.fragment_login, container, false);
    final Activity activity = getActivity();

    ... code ...

    return view;
}

I followed instructions from a tutorial someone suggested earlier from a different question, but I must be doing something horribly wrong. Any ideas?

xml file

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

your activity

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

 setContentView(R.layout.activity_login);

if (savedInstanceState == null) {
    FragmentLogin f1= new FragmentLogin();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.changeFragment, f1);
    fragmentTransaction.commit();

   }
}

I'm new to Android and I ran into the same problem. Here's how I fixed mine.

So in your onCreate method, use getSupportFragmentManager() from the FragmentManager class then declare FragmentTransaction separately. See below. I did this for my case and it worked.

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

setContentView(R.layout.activity_login);

if (savedInstanceState == null) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.login_screen_fragment, new FragmentLogin());
    fragmentTransaction.commit();
}

Hope this helps solve your problem as well.

Sincerely, sylvery

只需将fragment元素替换为FrameLayout,即可看到您的片段。

If you wanted to add the Fragment programatically, then remove the <Fragment> in your Framelayout and add some ID unto it.

It should look like this:

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

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