我知道这个问题在堆栈溢出中已被多次回答,但没有一个解决方案适合我。我需要从设备中选择一个pdf文件。调用startActivityResult(),我可以选择一个pdf文件,但是从不调用onActivityResult。奇怪的是,它在完美之前工作,但突然之间没有更多的响应。
MainActivity.java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
Fragment.java
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICKFILE_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null && data.getData() != null )
//do something
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
ConnectivityManager CM = (ConnectivityManager) getActivity().getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo ninfo = CM.getActiveNetworkInfo();
if (item.getItemId() == R.id.upload_prax && ninfo!=null && ninfo.isConnected() ) {
if (permissionexternal == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/pdf");
fragment.startActivityForResult(intent, PICKFILE_REQUEST_CODE);
}
答案 0 :(得分:3)
在您的片段中,使用
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
您无需为活动添加onActivityResult()
。只需删除
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
并在你的片段中,
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICKFILE_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null && data.getData() != null )
//do something
}
Toast.makeText(getActivity(), "On Activity Result Called", Toast.LENGTH_SHORT).show();
// add toast to check whether it is working or not
}
答案 1 :(得分:0)
您在片段中提出的请求。您可以在设置片段的活动中检索的请求的响应。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICKFILE_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null && data.getData() != null )
//do something
}
此代码应位于片段的父活动中。
答案 2 :(得分:0)
在活动中尝试此操作
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
if (fragment != null) {
switch (fragment.getTag()) {
case "Set your fragment name":
int PICKFILE_REQUEST_CODE = 138;
fragment.onActivityResult(PICK_IMAGE_SINGLE_MULTI, resultCode, data);
break;
}
}
}
答案 3 :(得分:-1)
似乎片段以某种方式覆盖requestCode。所以解决方案是这样做:
在活动中(托管片段的那个):
覆盖onActivityResult()
方法并致电super.onActivityResult
。呼叫超级是强制性的!否则它将不起作用。所以这是一个神奇的解决方案,在你的片段的托管活动中覆盖并调用super onActivityResult
方法。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Override this method in the activity that hosts the Fragment and call super
// in order to receive the result inside onActivityResult from the fragment.
super.onActivityResult(requestCode, resultCode, data);
}
在片段中
覆盖onActivityResult()
,让你的逻辑在这里(如果这是你想要的)
开始活动时,只使用startActivityForResult()
而非getActivity.startActivityForResult()
注意:如果您的活动扩展了其他活动,请确保父活动不会覆盖您的onActivityResult()
。