简体   繁体   English

尝试从活动开始片段。

[英]Trying to start a Fragment from an Activity.

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. 但我找不到任何执行此操作的示例。

Class Definition(Fragment1_2): 类定义(Fragment1_2):

public class Fragment1_2 extends Fragment {

Class Definition(AdhocEdit.class): 类定义(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));

Commented because none of them works. 评论,因为它们都不起作用。 Also do I need to add this Fragment1_2 into the Manifest, if so how? 我还需要将此Fragment1_2添加到清单中吗?

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. 您需要将片段添加到容器中。

Example from docs docs中的示例

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

fragment_container is the id of the container which is usually a FrameLayout and you add the desired fragment the container fragment_container是容器的ID,通常是一个FrameLayout,然后将所需的片段添加到容器中

More info @ 更多信息 @

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

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

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