如何获得从FragmentActivity片段实例? [英] How to get the fragment instance from the FragmentActivity?

查看:168
本文介绍了如何获得从FragmentActivity片段实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在FragmentActivity内容视图,该活动将按照在布局文件中指定的类名创建片段实例我。但是,我怎么能得到那个片段的实例吗?

 公共类MyActivity扩展FragmentActivity {

    @覆盖
    保护无效的onCreate(捆绑额外的){
        super.onCreate(额外);
        的setContentView(R.layout.page_fragment);
    }
}

< XML版本=1.0编码=UTF-8&GT?;
<片段的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:ID =@ + ID /浏览量
  机器人:layout_width =match_parent
  机器人:layout_height =match_parent
  机器人:名称=org.xi.android.PageFragment/>
 

解决方案

您可以使用 FragmentManager 使用 findFragmentById

由于您使用的是支持库(要扩展FragmentActivity),可以使用:

  getSupportFragmentManager()。findFragmentById(R.id.pageview)
 

有关蜂窝+设备使用

  getFragmentManager()。findFragmentById(R.id.pageview)
 

I set the content view in the FragmentActivity, and the activity will create the fragment instance for me according to the class name specified in the layout file. But how can I get that fragment instance?

public class MyActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle extra) {
        super.onCreate(extra);
        setContentView(R.layout.page_fragment);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/pageview"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:name="org.xi.android.PageFragment" />

解决方案

You can use use findFragmentById in FragmentManager.

Since you are using the Support library (you are extending FragmentActivity) you can use:

getSupportFragmentManager().findFragmentById(R.id.pageview)

For Honeycomb+ devices use

getFragmentManager().findFragmentById(R.id.pageview)

这篇关于如何获得从FragmentActivity片段实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆