永远不会调用我的onActivityResult方法。我正在使用android 2.2
我正在使用Tabhost,其中TabHosts包含包含单个活动的TabGroup。
我的一项活动运行以下意图
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), 0);
这会加载我的图库应用,我使用默认的Android图库来选择一个图像,当我返回时,我的onActivityResult不会被称为我的活动。
它看起来像这样 - 我在if(resultCode == 0)
放了一个断点,所以现在,我的onActivityResult的逻辑应该无关紧要
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 0) {
if (requestCode == 0) {
Uri selectedImageUri = data.getData();
//OI FILE Manager
filemanagerstring = selectedImageUri.getPath();
//MEDIA GALLERY
selectedImagePath = getPath(selectedImageUri);
//DEBUG PURPOSE - you can delete this if you want
if(selectedImagePath!=null)
System.out.println(selectedImagePath);
else System.out.println("selectedImagePath is null");
if(filemanagerstring!=null)
System.out.println(filemanagerstring);
else System.out.println("filemanagerstring is null");
//NOW WE HAVE OUR WANTED STRING
if(selectedImagePath!=null)
System.out.println("selectedImagePath is the right one for you!");
else
System.out.println("filemanagerstring is the right one for you!");
}
}
}
生命周期函数通常是乱序调用的,间歇性地调用tabhost / tabgroup中的活动,所以我检查了库关闭后调用的生命周期函数(一旦我从android库中选择一个图像就会发生这种情况) )
唯一被调用的是我的TabHost活动中的onResume()
。所以我尝试将完全相同的onActivityResult()
方法放在我的TabHost类中,以及TabActivity类。在方法开头的同一位置使用断点。
这两个类都没有被调用。
我现在正在绘制一个空白,如果内置的接收方法都没有响应,我怎样才能从我的应用中的图库应用中获得结果。
由于我知道我的主要TabHost被调用onResume()
,我尝试添加Intent graphics = getIntent();
以查看它是否会从图库选择中接收数据,但它没有,所以我不知道如何我可以在onResume()方法中执行逻辑。
欢迎解决方案! :)
答案 0 :(得分:18)
尝试使用包含当前活动的tabgroup活动的上下文来调用startActivityForResult,然后在tabgroup活动中进行监听。
使用它来获取tabGroupActivity:
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
然后从中调用startActivityForResult:
parentActivity.startActivityForResult(...);
最后,在tabGroupActivity:
中放置一个onActivityResult侦听器protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
}
答案 1 :(得分:16)
从许多问题来看,有很多原因导致被叫活动可能无法触发来电者的onActivityResult()
方法。
我发现的一个原因是,当我调用startActivityForResult(intent, requestCode)
时,requestCode
值小于0.我的应用程序不需要requestCode
,Android文档说使用< 0不会发送requestCode
。
但Android文档没有提到requestCode
<的结果。结果是它阻止调用者的onActivityResult()
方法被调用!哎哟!
因此,即使您的应用不需要requestCode
,您仍然希望使用值为> = 0的应用。
这就是我今天学到的: - )
答案 2 :(得分:4)
解决方案是在主要活动的顶部调用透明活动。此透明活动位于tabhost前面,并具有正常的生命周期功能。
这个透明活动调用了gallery onCreate(),它在onActivityResult中获得了正常返回的所有内容,并且您将能够像往常一样将返回的信息传递回应用程序的其余部分。 finish()位于onActivityResult方法的内部,因此用户甚至不会注意到调用了透明活动。
从评论中复制的更新:
活动A通过正常意图调用活动B.活动B没有xml并像这样运行onCreate
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.dialogpopper);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*"); startActivityForResult(intent, 0);
}//end onCreate
当活动C完成时,它会调用活动B的onActivityResult
答案 3 :(得分:2)
您只需要删除此表单中的android:noHistory="true"
。
答案 4 :(得分:1)
使用结果代码的常量值:
Activity.RESULT_OK和 Activity.RESULT_CANCELED
您会看到取消的值实际为0.因此,在您的代码中,您将检查活动是否已取消。
将您的代码更改为
if (resultCode == Activity.RESULT_OK) {
...
}
此外,将您的意图操作更改为:
intent.setAction(Intent.ACTION_PICK);
如果你这样做,你可以打电话
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 0);
而不是创建选择器。它将自动选择与该意图和mimetype相关的活动并将其显示给您
答案 5 :(得分:0)
调用onActivityResult的方式取决于Activity的launchMode(在清单中)。我不确定这可能是一个问题。
答案 6 :(得分:-1)
你的onActivityRestult上面有@Override吗?
(查看执行此操作的旧代码,因此不确定为何需要)调用super.onactivityresult(requestcode,resultscode,data)作为方法中的第一个调用
我的意图也没有其他内容 startActivityForResult(Intent.createChooser(意向, “选择图片”),0); 我认为应该是 startActivityForResult(source.class,destination.class); 当然,源和目的地应该是类的名称
public class ImageSwitcherView extends Activity {
int pics[] = { R.drawable.image000, R.drawable.image001,
R.drawable.image002};
private int currentIndex = 0;
SharedPreferences preferences;
Gallery gallery;
ImageView fullPicView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galleryview);
Bundle extras = getIntent().getExtras();
currentIndex = extras.getInt("bookmark");
gallery = (Gallery) findViewById(R.id.Gallery01);
gallery.setAdapter(new ImageAdapter(this));
gallery.setSelection(currentIndex);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
currentIndex = position;
// ---display the images selected---
fullPicView = (ImageView) findViewById(R.id.imageView1);
fullPicView.setImageResource(pics[currentIndex]);
fullPicView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(ImageSwitcherView.this,
imageView.class);
int resID = pics[currentIndex];
myIntent.putExtra("resID", resID);
myIntent.putExtra("index", currentIndex);
startActivityForResult(myIntent, 1);
}
});
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c) {
context = c;
// ---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
// ---returns the number of images---
public int getCount() {
return pics.length;
}
// ---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// ---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(pics[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
currentIndex = data.getIntExtra("bookmark", 0);
gallery.setSelection(currentIndex);
fullPicView.setImageResource(pics[currentIndex]);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
|| keyCode == KeyEvent.KEYCODE_HOME) {
preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("bookmark", gallery.getSelectedItemPosition());
editor.commit();
finish();
}
return super.onKeyDown(keyCode, event);
}
}