Java 从android中的片段调用活动

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

Call activity from fragment in android

javaandroidandroid-fragments

提问by Ando Masahashi

I have two classes:

我有两个班级:

One is Fragment1which containts fragment - like this:

一个是Fragment1包含片段 - 像这样:

public class Tab extends FragmentActivity

the second one is activity- like this:

第二个是activity- 像这样:

public class Wallpaper extends Activity

Now I want to start Activity from Fragment1how can I do this ?

现在我想从Fragment1如何开始活动?

采纳答案by Mohsen Afshin

Intent intent = new Intent(context, Wallpaper.class);

// intent.putExtra(....); // put your data

startActivity(intent);

回答by Tassos Bassoukos

See here: Android : Calling Activity from Fragment- the first result on Google searching for your question title...

请参阅此处:Android : Calling Activity from Fragment- Google 上搜索您的问题标题的第一个结果...

回答by Raghunandan

I want to start Activity from Fragment1 how can I do this ?

我想从 Fragment1 开始 Activity 我该怎么做?

You have this

你有这个

public class Tab extends FragmentActivityin which case you can use the below unless you are starting activity from fragment class

public class Tab extends FragmentActivity在这种情况下,除非您从片段类开始活动,否则您可以使用以下内容

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

Use the below in your fragment

在您的片段中使用以下内容

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

getActivity

getActivity

Return the Activity this fragment is currently associated with.

返回当前与此片段关联的 Activity。

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

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

There is an example in the docs check the same

文档中有一个示例检查相同

回答by Mahendran Candy

try this

尝试这个

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

Intent intent = new Intent(getActivity(), mFragmentFavorite.class); 开始活动(意图);