为什么在OnCreate startActivityForResult后调用的方法? [英] Why oncreate method called after startActivityForResult?
问题描述
在我的应用我想从图库
选择图像和的ListView
设置图像。在我的ListView 我有16行。所以,当过项目单击
的ListView 打开画廊和图像设置为
的ListView 。但我的问题是,在
startActivityForResult一些时间(),
的OnCreate()`方法调用。这是的onclick code
意向意图=新意图(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType(图像/ *);
startActivityForResult(
Intent.createChooser(意向,选择文件),
SELECT_PICTURE);
和这是的onActivityResult
公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
Log.e(结果,结果);
displayMetrics = this.getResources()getDisplayMetrics()。
EW = displayMetrics.widthPixels;
呃= displayMetrics.heightPixels; 开关(要求code){
案例SELECT_PICTURE:
如果(结果code == RESULT_OK){ lelListLayout.setVisibility(View.GONE);
relImageLayout.setVisibility(View.VISIBLE);
乌里selectedImageUri = data.getData();
selectedImagePath =的getPath(selectedImageUri); ExifInterface EXIF = NULL;
//位图bmRotated = NULL; 尝试{
EXIF =新ExifInterface(selectedImagePath);
}赶上(IOException异常E1){
// TODO自动生成catch块
e1.printStackTrace();
}
INT方向= exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Log.e(方向==>中,+方向); 尝试{ bmRotated = NULL;
bmGallayImage = NULL;
trimCache();
bmGallayImage = convertBitmap(selectedImagePath);
bmRotated = InventorySubmitImagesActivity.rotateBitmap(
bmGallayImage,方向); //如果(bmRotated.getWidth()> bmRotated.getHeight()){
如果(bmRotated.getWidth()> 1024){
浮X = 0;
X = 1024 /(浮点)bmRotated.getWidth();
// Log.e(X ====,值+ X); bmRotated = Bitmap.createScaledBitmap(bmRotated,1024,
(中间体)(bmRotated.getHeight()* x)中,真);
}
/ *
*}其他{如果(bmRotated.getHeight()> 1024){浮动X = 0;
* X = 1024 /(浮点)bmRotated.getHeight();
* Log.e(X ====,值+ X);
*
* bmRotated = Bitmap.createScaledBitmap(bmRotated,
*(INT)(bmRotated.getWidth()* X),1024,真正的); }}
* /
诶诶= - ll_buttonlayout.getHeight();
浮IW = bmRotated.getWidth();
浮IH = bmRotated.getHeight(); 浮动差异= E w的/ IW; 浮layoutwidth = E w的;
浮layoutheight =差异* IH; 如果(layoutheight> EH){ 差异=呃/ IH;
layoutwidth = E w的*差异;
layoutheight =诶;
} bmGallayImage = bmRotated;
如果(bmRotated!= NULL){ RelativeLayout.LayoutParams的LayoutParams =新RelativeLayout.LayoutParams(
(INT)layoutwidth,(INT)layoutheight);
relImage.setLayoutParams(的LayoutParams);
可绘制博士=新BitmapDrawable(bmRotated);
old_width = bmRotated.getWidth();
old_height = bmRotated.getHeight(); relImage.setBackgroundDrawable(DR); }
左=(INT)layoutwidth / 2 - 34;
顶=(int)的layoutheight / 2 - 8; }赶上(例外五){
// TODO自动生成catch块
e.printStackTrace();
}
// drag_check = TRUE;
relImage.removeAllViews(); imgMarker =新ImageView的(本);
最后RelativeLayout.LayoutParams的LayoutParams =新RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
relImage.addView(imgMarker,的LayoutParams);
imgMarker.setScaleType(ImageView.ScaleType.MATRIX);
bmdragImage = BitmapFactory.de codeResource(getResources()
R.drawable.image_marker);
imgMarker.setImageBitmap(bmdragImage); 矩阵=新的Matrix();
savedMatrix =新的Matrix();
oldDist = 0F;
启动=新的PointF();
中期=新的PointF(); matrix.postTranslate(左,顶部);
imgMarker.setImageMatrix(矩阵); imgMarker.setOnTouchListener(RefurbishmentImageActivity.this);
imgMarker.setVisibility(View.VISIBLE); // 结束..
//} }
打破;
}
}
在此的ListView
选择6或7影像后,onCreate方法被调用,在的onActivityResult()
。但经过的OnCreate()
方法再次startactivity结果调用。请指导我是什么问题。由于InAdvance所有..
在之前的onActivityResult()。但经过onCreate()方法再次startactivity结果叫做
块引用>在活动是发送到后台(当其他活动成为在它的上面,或者当它与home键发送背景),它的实例维持生命,只要系统不是在内存pressure。当系统没有足够的内存来做到这一点目前在做什么前景,它通常会停止并从内存中后台活动释放重新声明内存。
在这种情况下 - 系统为您提供<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)\"相对=nofollow>
Activity.onSaveInstanceState
回调这将从去活动调用被杀害,为您提供一个变化,以节省所需的任何状态它被杀害之前保存。当你的活动将回到前台 - 它将被重新创建(这就是为什么的的onCreate()称为再次),与savedInstanceState参数,将不能为null。
在savedInstanceState将与您在onSavedInstanceState()回调提供的所有额外捆绑。
这是非常重要的理解。
为了更好的理解,我建议你认真阅读 - 的http://developer.android.com/training/basics/activity-lifecycle/recreating.html
In my application i want to select image from
Gallery
and set the image inListView
. In my ListViewI have 16 rows. So when ever item click in
ListViewopen the gallery and set the image to
ListView. But my problem is some times after
startActivityForResult(),
oncreate()` method called. This is the onclick codeIntent intent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); startActivityForResult( Intent.createChooser(intent, "Select File"), SELECT_PICTURE);
And this is the onactivityresult
public void onActivityResult(int requestcode, int resultcode, Intent data) { Log.e("result", "result"); displayMetrics = this.getResources().getDisplayMetrics(); Ew = displayMetrics.widthPixels; Eh = displayMetrics.heightPixels; switch (requestcode) { case SELECT_PICTURE: if (resultcode == RESULT_OK) { lelListLayout.setVisibility(View.GONE); relImageLayout.setVisibility(View.VISIBLE); Uri selectedImageUri = data.getData(); selectedImagePath = getPath(selectedImageUri); ExifInterface exif = null; // Bitmap bmRotated = null; try { exif = new ExifInterface(selectedImagePath); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); Log.e("Orientation==>", "" + orientation); try { bmRotated = null; bmGallayImage = null; trimCache(); bmGallayImage = convertBitmap(selectedImagePath); bmRotated = InventorySubmitImagesActivity.rotateBitmap( bmGallayImage, orientation); // if(bmRotated.getWidth()>bmRotated.getHeight()){ if (bmRotated.getWidth() > 1024) { float x = 0; x = 1024 / (float) bmRotated.getWidth(); // Log.e("x====","value "+x); bmRotated = Bitmap.createScaledBitmap(bmRotated, 1024, (int) (bmRotated.getHeight() * x), true); } /* * }else{ if(bmRotated.getHeight() > 1024){ float x=0; * x=1024/(float)bmRotated.getHeight(); * Log.e("x====","value "+x); * * bmRotated = Bitmap.createScaledBitmap(bmRotated, * (int)(bmRotated.getWidth()*x), 1024, true); } } */ Eh = Eh - ll_buttonlayout.getHeight(); float iw = bmRotated.getWidth(); float ih = bmRotated.getHeight(); float diff = Ew / iw; float layoutwidth = Ew; float layoutheight = diff * ih; if (layoutheight > Eh) { diff = Eh / ih; layoutwidth = Ew * diff; layoutheight = Eh; } bmGallayImage = bmRotated; if (bmRotated != null) { RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( (int) layoutwidth, (int) layoutheight); relImage.setLayoutParams(layoutParams); Drawable dr = new BitmapDrawable(bmRotated); old_width = bmRotated.getWidth(); old_height = bmRotated.getHeight(); relImage.setBackgroundDrawable(dr); } left = (int) layoutwidth / 2 - 34; top = (int) layoutheight / 2 - 8; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // drag_check=true; relImage.removeAllViews(); imgMarker = new ImageView(this); final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); relImage.addView(imgMarker, layoutParams); imgMarker.setScaleType(ImageView.ScaleType.MATRIX); bmdragImage = BitmapFactory.decodeResource(getResources(), R.drawable.image_marker); imgMarker.setImageBitmap(bmdragImage); matrix = new Matrix(); savedMatrix = new Matrix(); oldDist = 0f; start = new PointF(); mid = new PointF(); matrix.postTranslate(left, top); imgMarker.setImageMatrix(matrix); imgMarker.setOnTouchListener(RefurbishmentImageActivity.this); imgMarker.setVisibility(View.VISIBLE); // end.. // } } break;
} }
In this
ListView
after selecting 6 or 7 images oncreate method called, before theonactivityresult()
. But afteroncreate()
method again startactivity result called. Please guide me what is the problem. Thanks InAdvance to all..解决方案before the onactivityresult(). But after oncreate() method again startactivity result called
when Activity is send to background (when other activity becomes on top of it, or when it sent with the home button to background) it instance kept alive as long as the system is not under memory pressure. when the system not have enough memory to do whatever it currently doing in foreground, it usually will re-claim memory by stopping and releasing from memory background activities.
in that case - the system provide you with the
Activity.onSaveInstanceState
callback which will be invoked from the activity that going to be killed to provide you a change to save any state needed to be saved before it been killed.when your activity will return to foreground - it will be re-created (that's why onCreate() called again), with savedInstanceState parameter that will not be null.
the savedInstanceState will hold bundle with all the extras you provided in the onSavedInstanceState() callback.
this is very important to understand.
for better understanding, I advise you to read seriously - http://developer.android.com/training/basics/activity-lifecycle/recreating.html
这篇关于为什么在OnCreate startActivityForResult后调用的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!