赞
踩
//启动其他Activity
void startActivity(Intent intent);
//指定请求码启动Activity,而且程序将会等到新启动的Activity的结果(重写onActivityResult方法获取)
void startActivityForResult(Intent intent, int requestCode);
/** Standard activity result: operation canceled. */
public static final int RESULT_CANCELED = 0;
/** Standard activity result: operation succeeded. */
public static final int RESULT_OK = -1;
//结束当前Activity
finish();
//结束以startActivityForResult(Intent intent, int requestCode)方式启动的Activity
finish(int requestCode);
//点击MainActivity的Button时调用 private void toSecondActivity() { Intent intent=new Intent(this,SecondActivity.class); //向Intent对象传入String类型数据 intent.putExtra("mainActivity","MainActivity传入的数据"); //构建Bundle对象,并传入数据 Bundle bundle=new Bundle(); String[] stringArray=new String[]{"hahaha","heiheihei","hehehehe","wawawawa"}; bundle.putStringArray("stringArray",stringArray); //将Bundle对象传入Intent intent.putExtras(bundle); //启动activity startActivityForResult(intent,0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode==0&&resultCode==RESULT_OK){ Log.d(TAG, "onActivityResult: "+data.getStringExtra("secondActivity")); } }
//获取启动该Activity之前的Activity对应的Intent Intent intent = getIntent(); //获取Intent对象携带的String类型数据 String value = intent.getStringExtra("mainActivity"); Log.d(TAG, "onCreate: " + value); //获取Intent携带的Bundle类型数据 Bundle bundle = intent.getExtras(); String[] array = bundle.getStringArray("stringArray"); for (int i = 0; i < array.length; i++) { Log.d(TAG, "onCreate: " + array[i]); } Button btnFinish = (Button) findViewById(R.id.btn_finish); btnFinish.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //传入字符串信息到Intent intent.putExtra("secondActivity", "SecondActivity传入的信息"); //设置该SecondActivity的结果码,并设置结束之后退回的Activity setResult(RESULT_OK, intent); //结束SecondActivity finish(); } });
/*跳转到SecondActivity*/
SecondActivity: onCreate: MainActivity传入的数据
SecondActivity: onCreate: hahaha
SecondActivity: onCreate: heiheihei
SecondActivity: onCreate: hehehehe
SecondActivity: onCreate: wawawawa
/*SecondActivity被销毁*/
MainActivity: onActivityResult: SecondActivity传入的信息
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(intent,REQUEST_FLAG_TAKE_PHOTO); private final int REQUEST_FLAG_TAKE_PHOTO = 101;;//自定义 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_FLAG_TAKE_PHOTO) { if (resultCode == RESULT_OK) { //operation succeeded if (data.getData() != null || data.getExtras() != null) { //防止没有返回结果 Uri uri = data.getData(); if (uri != null) { //拿到图片 photo = BitmapFactory.decodeFile(uri.getPath()); } if (photo == null) { Bundle bundle = data.getExtras(); if (bundle != null) { photo = (Bitmap) bundle.get("data"); ivPhoto.setImageBitmap(photo); } else { Toast.makeText(getApplicationContext(), "找不到图片", Toast.LENGTH_SHORT).show(); } } //处理图片 //裁剪图片 } Log.d(TAG, "SecondActivity: operation succeeded"); } else if (resultCode == RESULT_CANCELED) { //operation canceled Log.d(TAG, "SecondActivity: operation canceled"); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。