onActivityResult()不会调用新的嵌套片段API [英] onActivityResult() not called in new nested fragment API
问题描述
我一直在使用新的嵌套的片段的API,Android包含在支持库。
这我面临具有嵌套片段的问题是,如果一个嵌套片段(即,已通过 FragmentManager
返回的被添加到另一个片段的片段 getChildFragmentManager()
)调用 startActivityForResult()
,嵌套片段的 onActivityResult()
方法不叫。但是,无论是父片段的 onActivityResult()
和活动的 onActivityResult()
就被调用。
我不知道如果我失去了一些东西有关嵌套的片段,但我没想到所描述的行为。下面是code重现这个问题。我非常AP preciate如果有人能指出我朝着正确的方向,向我解释什么,我做错了:
包com.example.nestedfragmentactivityresult;
进口android.media.RingtoneManager;
进口android.os.Bundle;
进口android.content.Intent;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.Toast;
公共类MainActivity扩展FragmentActivity
{
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getSupportFragmentManager()
.beginTransaction()
。新增(android.R.id.content,新ContainerFragment())
。承诺();
}
公共无效onActivityResult(INT申请code,INT结果code,意图数据)
{
super.onActivityResult(要求code,因此code,数据);
//这就是所谓的
Toast.makeText(getApplication(),
消耗的活动,
Toast.LENGTH_SHORT).show();
}
公共静态类ContainerFragment扩展片段
{
公共最后查看onCreateView(LayoutInflater充气,
集装箱的ViewGroup,
捆绑savedInstanceState)
{
查看结果= inflater.inflate(R.layout.test_nested_fragment_container,
容器,
假);
返回结果;
}
公共无效onActivityCreated(包savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
getChildFragmentManager()的BeginTransaction()
。新增(R.id.content,新NestedFragment())
。承诺();
}
公共无效onActivityResult(INT申请code,
INT结果code,
意图数据)
{
super.onActivityResult(要求code,因此code,数据);
//这就是所谓的
Toast.makeText(getActivity(),
消耗的父母片段,
Toast.LENGTH_SHORT).show();
}
}
公共静态类NestedFragment扩展片段
{
公共最后查看onCreateView(LayoutInflater充气,
集装箱的ViewGroup,
捆绑savedInstanceState)
{
Button按钮=新按钮(getActivity());
button.setText(点击我!);
button.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v)
{
意向意图=新的意图(RingtoneManager.ACTION_RINGTONE_PICKER);
startActivityForResult(意向,0);
}
});
返回按钮;
}
公共无效onActivityResult(INT申请code,
INT结果code,
意图数据)
{
super.onActivityResult(要求code,因此code,数据);
//这不叫
Toast.makeText(getActivity(),
消耗嵌套的片段,
Toast.LENGTH_SHORT).show();
}
}
}
test_nested_fragment_container.xml是:
< XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /内容
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< /的FrameLayout>
是的, onActivityResult()
在嵌套的片段将不会通过这样的方式来调用。
onActivityResult的调用顺序(在Android的支持库)是
-
Activity.dispatchActivityResult()
。 -
FragmentActivity.onActivityResult()
。 -
Fragment.onActivityResult()
。
在第3步,该片段是在父的 FragmentMananger
活动
找到。因此,在你的榜样,它是发现派遣 onActivityResult()
,嵌套的片段可能永远不会收到该事件的容器碎片。
我认为你必须实现自己的调度ContainerFragment.onActivityResult()
,找到嵌套的片段,并调用结果和数据传递给它。
I have been using the new nested fragment API that Android includes in the support library.
The problem that I am facing with nested fragments is that, if a nested fragment (that is, a fragment that has been added to another fragment via the FragmentManager
returned by getChildFragmentManager()
) calls startActivityForResult()
, the nested fragment's onActivityResult()
method is not called. However, both the parent fragment's onActivityResult()
and activity's onActivityResult()
do get called.
I don't know if I am missing something about nested fragments, but I did not expect the described behavior. Below is the code that reproduces this problem. I would very much appreciate if someone can point me in the right direction and explain to me what I am doing wrong:
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 is:
<?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>
Yes, the onActivityResult()
in nested fragment will not be invoked by this way.
The calling sequence of onActivityResult (in Android support library) is
Activity.dispatchActivityResult()
.FragmentActivity.onActivityResult()
.Fragment.onActivityResult()
.
In the 3rd step, the fragment is found in the FragmentMananger
of parent Activity
. So in your example, it is the container fragment that is found to dispatch onActivityResult()
, nested fragment could never receive the event.
I think you have to implement your own dispatch in ContainerFragment.onActivityResult()
, find the nested fragment and invoke pass the result and data to it.
这篇关于onActivityResult()不会调用新的嵌套片段API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!