简体   繁体   English

活动启动时的默认片段

[英]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: 活动文件的onCreate方法:

@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: 活动的XML:

<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): 和Fragment类(相关部分):

@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 xml文件

<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. 我是Android的新手,但遇到了同样的问题。 Here's how I fixed mine. 这是我固定我的方法。

So in your onCreate method, use getSupportFragmentManager() from the FragmentManager class then declare FragmentTransaction separately. 因此,在onCreate方法中,使用FragmentManager类中的getSupportFragmentManager(),然后分别声明FragmentTransaction。 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. 如果要以编程方式添加片段,请在帧布局中删除<Fragment>并为其添加一些ID。

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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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