在具有Intent.ACTION_GET_CONTENT的startActivityForResult()之后未调用onActivityResult() [英] onActivityResult() not called after startActivityForResult() with Intent.ACTION_GET_CONTENT
问题描述
我的主Activity
拥有不同的Fragment
,一个片段使用户可以打开DialogFragment
.该对话框将打开一个声音文件列表,并且该对话框还包含一个添加"按钮,用户应该可以从中添加自己的声音文件.为此,我想使用标准的Android文件选择功能并使用Intent.ACTION_GET_CONTENT
.因此,当用户按下添加"按钮,并且按需启动Android文件选择功能时,我就消除了这种意图,但是当我选择一个文件时,什么也没有发生.我希望可以触发我的主要活动的onActivityResult()
,但是它不会触发,并且我无法使其正常工作,并且我不理解为什么这不起作用.
I got my main Activity
which holds different Fragment
's, one fragment gives the user a possibility to open a DialogFragment
. That dialog opens a list of sound files and the dialog also contains a 'Add' button from which the user should be able to add her own soundfile. To do this I thought to use the standard Android file picking functionality and make use of the Intent.ACTION_GET_CONTENT
. So I fire away this intent when the user presses the 'Add' button and the Android file picking functionality comes up as it should, but when I choose a file, nothing happens what so ever. I would expect the onActivityResult()
of my main activity to be triggered but it doesn't, and I can't get it to work and I don't understand why this doesn't work.
public class MyDialog extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Rest omitted...
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(context)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle(R.string.TONE_PROMPT_TITLE)
.setNeutralButton(R.string.ADD, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/mpeg");
getActivity().startActivityForResult(intent, ADD_SOUND_REQUEST_CODE);
}
})
.create();
}
}
我还试图通过以下方式实例化意图:
Intent intent = new Intent(getActivity(), MyActivity.class);
但这也不起作用.
I have also tried to instantiate the intent the following way:
Intent intent = new Intent(getActivity(), MyActivity.class);
but this doesn't work either.
任何想法都会非常有帮助,谢谢!
Any ideas would be really helpful, thanks!
推荐答案
设法找出问题所在...
在清单中,我已将Activity
声明为android:noHistory="false"
,这就是问题所在.将其设置为android:noHistory="true"
以进行我的活动并按以下方式触发Intent
后,它就像一种魅力一样工作.
Managed to figure out what the problem was...
In my manifest I have declared my Activity
as android:noHistory="false"
and this was the problem. After setting it to android:noHistory="true"
for my activity and firing my Intent
the following way it worked like a charm.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/mpeg");
getActivity().startActivityForResult(intent, ADD_SOUND_REQUEST_CODE);
就像这样,我的活动中的onActivityResult()
会被触发,此后,我遵循@ Rohit5k2的关于如何触发Fragment
的OnActivityResult()的建议,并且一切都非常好!
And just like that, the onActivityResult()
in my activity was triggered just as it should and after that I followed @Rohit5k2's advice on how to get my Fragment
's OnActivityResult() triggered and all has worked really great!
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
MyFragment fragmentObject = getSupportFragmentManager().findFragmentById(R.id.container);
fragmentObject.onActivityResult(requestCode, resultCode, data);
}
谢谢大家的帮助!
这篇关于在具有Intent.ACTION_GET_CONTENT的startActivityForResult()之后未调用onActivityResult()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!