android.support.v4.app.FragmentManager 还是 android.app.FragmentManager?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23044826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:43:25  来源:igfitidea点击:

android.support.v4.app.FragmentManager OR android.app.FragmentManager?

androidandroid-fragmentsfragmentfragmentmanager

提问by Foo

I'm learning Android development. I get stuck at something that should be very easy.

我正在学习Android开发。我陷入了一些应该很容易的事情。

Im creating an App with one Activity, 2 fragments and 1 interface.

我正在创建一个包含一个活动、2 个片段和 1 个界面的应用程序。

android:minSdkVersion="11"
android:targetSdkVersion="19

So in the main activity Im trying to create a reference to Fragment B using the manager. I get stuck here, because Eclispse is telling me to change some things (see below):

因此,在主要活动中,我尝试使用管理器创建对片段 B 的引用。我被困在这里,因为 Eclispse 告诉我改变一些事情(见下文):

My intension:`

我的意图:`

@Override
    public void respond(int i) {
        // TODO Auto-generated method stub

    FragmentManager manager =getFragmentManager();
    FragmentB f2= (FragmentB) manager.findFragmentById(R.id.fragment2);

}`

If I do it this whay I get the error messages and need to perform some changes. After the changes the code looks like this (and I still can't reach FragmentB):

如果我这样做,我会收到错误消息并需要执行一些更改。更改后的代码如下所示(我仍然无法访问 FragmentB):

    @Override
public void respond(int i) {
    // TODO Auto-generated method stub

    android.app.FragmentManager manager =getFragmentManager();
    android.app.Fragment f2=  manager.findFragmentById(R.id.fragment2);

}

For extra details I'll put here also the import header of the Activity:

有关更多详细信息,我还将在此处放置活动的导入标头:

  package com.example.modular_ui;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends Activity implements Communicator{....

What am I missing here? the whole support.v4 /support.v7 thing is a little confusing for rookies.

我在这里缺少什么?整个 support.v4 /support.v7 对新手来说有点混乱。

EDIT: After changing to:

编辑:更改为:

    import android.app.Fragment;
import android.app.FragmentManager;

AND extending FragmentActivity I still can't create a reference to FragmentB:

并且扩展 FragmentActivity 我仍然无法创建对 FragmentB 的引用:

@Override
public void respond(int i) {
    // TODO Auto-generated method stub

FragmentManager man = getFragmentManager();
FragmentB b = man.findFragmentById(R.id.fragment2);

}

}

As Requested I've posted the FragmentB code:

根据要求,我发布了 FragmentB 代码:

package com.example.modular_ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentB extends Fragment {

TextView text;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.fragment_b, container);
}

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        text = (TextView) getActivity().findViewById(R.id.textView1);
    }

Main XML

主 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.modular_ui.MainActivity"
    tools:ignore="MergeRootFrame" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.modular_ui.FragmentA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.modular_ui.FragmentB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/fragment1"
        android:layout_marginTop="54dp" />

</RelativeLayout>

采纳答案by RobertM

First of all: your activity should extend FragmentActivity.

首先:您的活动应该扩展 FragmentActivity。

About support libraries. They were introduced to add some functionalities to older Androids. For example Fragments were introduced in Android 3.0 (SDK nr: 11). In fact (according to documentation) in Androids 3.0 < support libary uses system implementation of Fragments.

关于支持库。引入它们是为了向旧版 Android 添加一些功能。例如,Android 3.0 (SDK nr: 11) 中引入了 Fragment。事实上(根据文档)在 Androids 3.0 < 支持库中使用 Fragments 的系统实现。

回答by bgauryy

Simply use getSupportFragmentManager(); , after you added the support library successfully.

只需使用 getSupportFragmentManager(); , 成功添加支持库后。

回答by ToolmakerSteve

OP was so close to having a solution that would have worked fine for API 11 and newer without needing support.v4.

OP 非常接近于拥有一个可以在不需要 support.v4 的情况下对 API 11 和更新版本正常工作的解决方案。

He just needed to change his Fragmentto also not use support.v4, in its importstatement.

他只需要在导入语句中更改他的Fragment以不使用 support.v4 。

Summary of the two approaches. ALL your Activities and Fragmentsshould have code that looks like exactly oneof these; do not mix them! (Not all lines are needed in all files; use lines as needed.)

两种方法的总结。 你所有的活动和片段都应该有看起来像其中之一的代码;不要混合它们!(并非所有文件中都需要所有行;根据需要使用行。)

support-v4 approach

支持-v4 方法

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;      <-- ".support.v4"
import android.support.v4.app.FragmentManager;

... MainActivity extends FragmentActivity ...

... = getSupportFragmentManager();

.... YourFragment extends Fragment ... <-- same in both approaches

API 11+ approach

API 11+ 方法

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;

... MainActivity extends Activity ...

... = getFragmentManager();

.... YourFragment extends Fragment ... <-- same in both approaches


So if you have a project that is written using one approach above, and you are integrating in code from elsewhere, be sure to look for these lines, and change them to match what you have.

因此,如果您有一个使用上述一种方法编写的项目,并且您正在从其他地方集成代码,请务必查找这些行,并更改它们以匹配您拥有的内容。

回答by suresh yadam

It's simple.

这很简单。

If you also want your app to run in older devices (below API level 11), use getSupportFragmentManager().

如果您还希望您的应用程序在较旧的设备上运行(低于 API 级别 11),请使用getSupportFragmentManager().

If you want your app to run in devices with API Level above 11, then use getFragmentManger().

如果您希望您的应用在 API 级别高于 11 的设备上运行,请使用 getFragmentManger().