我希望第二项活动是ShowActivity
当我按回它时会返回ArrayList
,但我无法得到它。在调试setResult()
方法中显示数组,但在activityResult
数据的下一个点为空为什么?
在第一个活动中,onActivityResult
数据为空。
MainActivity.java:
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent show=new Intent(MainActivity.this,ShowActivity.class);
// Create a Bundle and Put Bundle in to it
Bundle bundleObject = new Bundle();
bundleObject.putSerializable("key", showImagelist);
show.putExtras(bundleObject);
//startActivity(show);
startActivityForResult(show,COUNTER_ARRAY_LIST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==SELECT_IMAGE){
if(resultCode==RESULT_OK && null!=data){
for (int i=0,c=0,r=0;i<count;i++,c++){
if(c==coloumn){
c=0;
r++;
}
}
ImageUri=data.getData();
imageItemBin=new ImageItemBin();
imageItemBin.setImage(ImageUri.toString());
publicSelectedImage.add(imageItemBin);
gridAdaptor=new CustomAdapter(MainActivity.this,publicSelectedImage,this);
gridview.setAdapter(gridAdaptor);
}
}
if(requestCode==REQUEST_CAMERA)
{
if(resultCode==RESULT_OK && data!=null){
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
String stringImage=BitMapToString(mphoto);
imageItemBin=new ImageItemBin();
imageItemBin.setImage(stringImage);
//imageItemBin.setImage(picturePth.toString());
publicSelectedImage.add(imageItemBin);
gridAdaptor=new CustomAdapter(MainActivity.this,publicSelectedImage,this);
gridview.setAdapter(gridAdaptor);
//gridAdaptor.notifyDataSetChanged();
// tvcounter.setText(counter);
//publicSelectedImage=selectedImage;
}
}
if(requestCode==COUNTER_ARRAY_LIST){
//if(resultCode==RESULT_OK && data!=null){
Bundle bundle=getIntent().getExtras();
showImagelist=(ArrayList<ImageItemBin>)bundle.getSerializable("BackArrayList");
int sizeOfArrry=showImagelist.size();
btnShow.setText("Show ( " + Integer.toString(sizeOfArrry) + " ) ");
showImagelist.notifyAll();
//}
}
}
ShowActivity.java:
@Override
public void onBackPressed() {
super.onBackPressed();
Intent back=new Intent();
Bundle b=new Bundle();
b.putSerializable("BackArrayList",showlist1);
back.putExtras(b);
//startActivity(back,b);
setResult(2,back);
finish();
//startActivityForResult(back,MainActivity.COUNTER_ARRAY_LIST,b);
}
答案 0 :(得分:0)
为什么必须在方法super.onBackPressed()
执行之前设置结果:
onBackPressed()
的代码
如果backstack上没有可用的片段,则此方法将调用方法finishAfterTransition()
。在finishAfterTransition()
方法中,您会看到对finish()
的调用。因此,在您在代码中设置结果之前,系统会将活动移至状态&#34;已完成&#34;然后忽略后面的设置结果。
因此,您必须确保在调用super.onBackPressed()
之前设置结果,以确保在活动结束之前设置结果。