android - 根据哪个 Activity 包含 fragment 执行 Intent

标签 android android-fragments android-intent

你好,我制作了一个只有一个按钮的 fragment ,我在两个 Activity 中使用它。由此产生的问题是 fragment 将执行相同的 Intent

Onclick(类型变量是它应该运行的 Activity 并且没有实现到onclick中,这不起作用):

runBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
if (type == 0){
        Intent sendToMain = new Intent(getActivity(), MainActivity.class);
        sendToMain.putExtra("newStory", story);

    }
    if (type ==1){
        Intent reviewStoryIntent = new Intent(getActivity(),storyReviewActivity.class);
        reviewStoryIntent.putExtra("Story", story);
    }

        }
    });

有什么方法可以区 fragment 段放在哪个 Activity 中吗?

最佳答案

您可以在您的 fragment 中检查您的不同 Activity ,如下所示:

if(getActivity() instanceof YourFirstActivity) // type == 0
{
  // Do your stuff here for First Activity

}else if(getActivity() instanceof YourSecondActivity) // type ==1
{
  // Do your stuff here for Second Activity
}

关于android - 根据哪个 Activity 包含 fragment 执行 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43720185/

相关文章:

android - SearchView 展开后隐藏的操作栏图标

android - .Apk 在我的系统上很大

android - Mapsforge 的 fragment 支持

android - 配置更改后 Fragment.getArguments() 是否返回传递的参数?

android - Intent 值未传递回启动 Activity

android - 如何在 android Activity 完成后刷新 fragment ?

java - 错误 'S' 不是有效的?

Java继承: is the parent class instantiantiated every time a child class extends it?

android - 从 Intent 过滤器再次启动根 Activity 时处理根 Activity 的现有实例

android - 为 Android 编译 Irrlicht 时出现错误