gpt4 book ai didi

java - onActivityResult() 未在新的嵌套 fragment API 中调用

转载 作者:IT老高 更新时间:2023-10-28 13:11:33 25 4
gpt4 key购买 nike

我一直在使用新的nested fragment Android 包含在支持库中的 API。

我面临的嵌套 fragment 的问题是,如果嵌套 fragment (即通过 getChildFragmentManager() 返回的 FragmentManager 添加到另一个 fragment 的 fragment ) 调用startActivityForResult(),嵌套 fragment 的onActivityResult() 方法没有被调用。但是,父 fragment 的 onActivityResult() 和 Activity 的 onActivityResult() 都会被调用。

我不知道我是否遗漏了有关嵌套 fragment 的某些内容,但我没想到所描述的行为。下面是重现此问题的代码。如果有人能指出我正确的方向并向我解释我做错了什么,我将不胜感激:

package com.example.nestedfragmentactivityresult;

import android.media.RingtoneManager;
import android.os.Bundle;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends FragmentActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, new ContainerFragment())
.commit();
}

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);

// This is called
Toast.makeText(getApplication(),
"Consumed by activity",
Toast.LENGTH_SHORT).show();
}

public static class ContainerFragment extends Fragment
{
public final View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View result = inflater.inflate(R.layout.test_nested_fragment_container,
container,
false);

return result;
}

public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
getChildFragmentManager().beginTransaction()
.add(R.id.content, new NestedFragment())
.commit();
}

public void onActivityResult(int requestCode,
int resultCode,
Intent data)
{
super.onActivityResult(requestCode, resultCode, data);

// This is called
Toast.makeText(getActivity(),
"Consumed by parent fragment",
Toast.LENGTH_SHORT).show();
}
}

public static class NestedFragment extends Fragment
{
public final View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
Button button = new Button(getActivity());
button.setText("Click me!");
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
startActivityForResult(intent, 0);
}
});

return button;
}

public void onActivityResult(int requestCode,
int resultCode,
Intent data)
{
super.onActivityResult(requestCode, resultCode, data);

// This is NOT called
Toast.makeText(getActivity(),
"Consumed by nested fragment",
Toast.LENGTH_SHORT).show();
}
}
}

test_nested_fragment_container.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</FrameLayout>

最佳答案

我用以下代码解决了这个问题(使用了支持库):

在容器 fragment 中以这种方式覆盖onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

List<Fragment> fragments = getChildFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}

现在嵌套 fragment 将接收到 onActivityResult 方法的调用。

另外,如 noted Eric Brynsvold在类似的问题中,嵌套 fragment 应该使用它的父 fragment 而不是简单的 startActivityForResult() 调用来启动 Activity 。因此,在嵌套 fragment 开始 Activity 中:

getParentFragment().startActivityForResult(intent, requestCode);

关于java - onActivityResult() 未在新的嵌套 fragment API 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13580075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com