试图从开始一个活动的片段。 [英] Trying to start a Fragment from an Activity.

查看:88
本文介绍了试图从开始一个活动的片段。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用操作栏选项卡已经实现标签视图,现在也有它离开此选项卡视图一两页。在某一点的时候,我想再次呼吁在前台的片段之一。但我没有找到如何做到这一点的任何例子。

I have a tabbed view which I have implemented using Action Bar tabs, now there are one or two pages which navigate away from this tab view. At some point of time I want to call one of the fragments in the foreground again. But I am not finding any example of how to do this.

类定义(Fragment1_2):

Class Definition(Fragment1_2):

public class Fragment1_2 extends Fragment {

类定义(AdhocEdit.class):

Class Definition(AdhocEdit.class):

public class AdhocEdit extends Activity{

活动到碎片意图至今:

Activity to Fragment Intent so far:

Fragment1_2 fragmentB = (Fragment1_2)getFragmentManager().findFragmentById(R.id.fragemnt1_2);

        /*  Intent mainIntent;
            mainIntent = new Intent(AdhocEdit.this,Fragment1_2.class); 
            AdhocEdit.this.startActivity(mainIntent);
            AdhocEdit.this.finish();    */


        //    startActivity(new Intent(AdhocEdit.this, Fragment1_2.class));

评论,因为他们没有工作。另外,我需要添加此Fragment1_2入清单,如果是的话怎么样?

Commented because none of them works. Also do I need to add this Fragment1_2 into the Manifest, if so how?

推荐答案

您不能启动一个片段像你这样的活动。

You can't start a fragment like you do for activity.

片段是由活动主办。您需要将片段添加到容器。

Fragment is hosted by a activity. You need to add the fragment to the container.

从文档实例

ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

fragment_container 是容器通常是一个的FrameLayout的ID,并添加所需片段容器

fragment_container is the id of the container which is usually a FrameLayout and you add the desired fragment the container

更多信息@

http://developer.android.com/guide/components/fragments.html

这篇关于试图从开始一个活动的片段。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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