Android 如何从 Fragment 中开始活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12074608/
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 09:40:16  来源:igfitidea点击:

How do I start an activity from within a Fragment?

androidandroid-activityfragment

提问by

I have a set of tabs inside of a FragmentActivitythat each hold their own fragment. When I tried to start a new activity from within that fragment via an onClickListener, and using the startActivity(myIntent)method, my application force closes.

我在 a 中有一组标签FragmentActivity,每个标签都有自己的片段。当我尝试通过onClickListener, 并使用该startActivity(myIntent)方法从该片段中启动新活动时,我的应用程序强制关闭。

After looking around for a while, I found a reference or two to a method called startActivityFromFragment, but after searching around for an hour or so I can't find any explanations or examples of how to use it or whether this is what I should be using.

环顾了一段时间后,我找到了一两个名为 的方法的参考startActivityFromFragment,但在搜索了一个小时左右后,我找不到任何关于如何使用它的解释或示例,或者这是否是我应该使用的方法.

I guess what I'm asking is whether there is any difference between launching a new activity from an activity, and launching a new activity from a fragment, and if so, what do I need to implement?

我想我要问的是从活动启动新活动和从片段启动新活动之间是否有任何区别,如果有,我需要实现什么?

回答by EkKoZ

You should do it with getActivity().startActivity(myIntent)

你应该这样做 getActivity().startActivity(myIntent)

回答by Jayesh

I done it, below code is working for me....

我做到了,下面的代码对我有用....

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.hello_world, container, false);

        Button newPage = (Button)v.findViewById(R.id.click);
        newPage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HomeActivity.class);
                startActivity(intent);
            }
        });
        return v;
    }

and Please make sure that your destination activity should be register in Manifest.xml file,

并请确保您的目标活动应在 Manifest.xml 文件中注册,

but in my case all tabs are not shown in HomeActivity, is any solution for that ?

但在我的情况下,HomeActivity 中没有显示所有选项卡,有什么解决方案吗?

回答by David Dostal

The difference between starting an Activity from a Fragment and an Activity is how you get the context, because in both cases it has to be an activity.

从 Fragment 启动 Activity 和从 Activity 启动 Activity 的区别在于获取上下文的方式,因为在这两种情况下,它都必须是一个 Activity。

From an activity:The context is the current activity (this)

来自活动:上下文是当前活动 ( this)

Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);

From a fragment:The context is the parent activity (getActivity()). Notice, that the fragment itself can start the activity via startActivity(), this is not necessary to be done from the activity.

来自片段:上下文是父活动 ( getActivity())。请注意,片段本身可以通过 启动活动startActivity(),这不是必须从活动中完成的。

Intent intent = new Intent(getActivity(), NewActivity.class);
startActivity(intent);

回答by fadedbee

I do it like this, to launch the SendFreeTextActivity from a (custom) menu fragment that appears in multiple activities:

我这样做是为了从出现在多个活动中的(自定义)菜单片段启动 SendFreeTextActivity:

In the MenuFragment class:

在 MenuFragment 类中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_menu, container, false);

    final Button sendFreeTextButton = (Button) view.findViewById(R.id.sendFreeTextButton);
    sendFreeTextButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d(TAG, "sendFreeTextButton clicked");
            Intent intent = new Intent(getActivity(), SendFreeTextActivity.class);
            MenuFragment.this.startActivity(intent);
        }
    });
    ...

回答by user2766004

Use the Base Context of the Activity in which your fragment resides to start an Intent.

使用您的片段所在的活动的基本上下文来启动一个意图。

Intent j = new Intent(fBaseCtx, NewactivityName.class);         
startActivity(j);

where fBaseCtxis BaseContextof your current activity. You can get it as fBaseCtx = getBaseContext();

这里fBaseCtxBaseContext您当前的活动。你可以把它作为fBaseCtx = getBaseContext();