1. 程式人生 > >Fragment中startActivityForResult與onActivityResult詳細解決方案

Fragment中startActivityForResult與onActivityResult詳細解決方案

首先應該在Activity中用frament引用一下onActivityResult(),例如fragmentdemo. onActivityResult(),這裡不用寫父類的super. onActivityResult(),

<span style="white-space:pre">	</span>public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		<span style="color:#ff6666;">cuPhotoVoiceItem</span>.onActivityResult(requestCode, resultCode, data);
		Log.e(getClass().getName()+"onActivityResult", getClass().getName()+"onActiv<span style="white-space:pre">		</span>ityResult");
		
	}

這裡要記住,fragmentdemo是繼承了fragment的一個物件,在它相應的fragment類中要寫onActivityResult的真正要跳轉的具體類容,這裡要寫父類的super. onActivityResult(),

public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		<span style="color:#cccccc;background-color: rgb(255, 0, 0);">super.onActivityResult(requestCode, resultCode, data);</span>
		Log.e(getClass().getName()+"onActivityResult", getClass().getName()+"onActivityResult");
		if (resultCode == Activity.RESULT_OK && requestCode == 1) {
			Bundle b = new Bundle();
			b.putString("imagePath", photo1RealPath);
			Intent intent = new Intent(context, ChoosephotoActivity.class);
			flag = 1;
			intent.putExtras(b);
			((FragmentActivity) context).startActivityForResult(intent, 5);
		} else if (resultCode == Activity.RESULT_OK && requestCode == 2) {
			Bundle bundle = new Bundle();
			Uri uri = data.getData();

			String[] filePathColumn = { MediaStore.Images.Media.DATA };
			Log.e("picture", uri.toString());
			Cursor cursor = context.getContentResolver().query(uri,
					filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			photo1RealPath = cursor.getString(columnIndex);
			cursor.close();
			bundle.putString("imagePath", photo1RealPath);
			Intent intent = new Intent(context, ChoosephotoActivity.class);
			flag = 2;
			intent.putExtras(bundle);
			((FragmentActivity) context).startActivityForResult(intent, 5);
		}

		else if (resultCode == Activity.RESULT_OK && requestCode == 3) {
			Bundle b1 = new Bundle();
			b1.putString("imagePath", photo2RealPath);
			Intent intent1 = new Intent(context, ChoosephotoActivity.class);
			flag = 1;
			intent1.putExtras(b1);
			((FragmentActivity) context).startActivityForResult(intent1, 6);
		} else if (resultCode == Activity.RESULT_OK && requestCode == 4) {
			Bundle bundle = new Bundle();
			Uri uri = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };
			Log.e("picture", uri.toString());
			Cursor cursor = context.getContentResolver().query(uri,
					filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			photo2RealPath = cursor.getString(columnIndex);
			cursor.close();
			bundle.putString("imagePath", photo2RealPath);
			Intent intent = new Intent(context, ChoosephotoActivity.class);
			flag = 2;
			intent.putExtras(bundle);
			((FragmentActivity) context).startActivityForResult(intent, 6);
		} else if (resultCode == Activity.RESULT_OK && requestCode == 5) {
			photo1Path = data.getStringExtra("imagePath");
			photo1RealPath = Config.IMAGE_PATH + photo1Path;
		} else if (resultCode == Activity.RESULT_OK && requestCode == 6) {
			photo2Path = data.getStringExtra("imagePath");
			photo2RealPath = Config.IMAGE_PATH + photo2Path;
		} else if (resultCode == Activity.RESULT_OK && requestCode == 7) {
			voicePath = data.getStringExtra("voiceName");
			voiceTimeStr = data.getIntExtra("voiceTime", 0) + "'";
		}
		if (!photo1Path.equals("")) {
			badgeImage1.show();
			badgeImage1.setOnClickListener(badgeImage1Listener);
			ImageLoader.getInstance()
					.displayImage("file://" + Config.IMAGE_PATH + photo1Path,
							photo1, options);
			context.getResources().getDrawable(
					R.drawable.defect_compose_pic_add_highlighted);
			Log.e("獲取到的photo1Path", "photo1Path"+photo1Path);
			photo2.setVisibility(View.VISIBLE);
			photo2.setOnClickListener(this);
		}
		if (!photo2Path.equals("")) {
			badgeImage1.show();
			badgeImage1.setOnClickListener(badgeImage2Listener);
			ImageLoader.getInstance()
					.displayImage("file://" + Config.IMAGE_PATH + photo2Path,
							photo2, options);
			Log.e("獲取到的photo2Path", "photo2Path"+photo2Path);
		}

		if (!voicePath.equals("")) {
			voice.setBackground(context.getResources().getDrawable(
					R.drawable.record_voice));
			//initBadgeView(badgeVoice);
			badgeVoice.show();
			badgeVoice.setOnClickListener(badgeVoiceListener);
			voiceTime.setVisibility(View.VISIBLE);
			voiceTime.setText(voiceTimeStr + "'");
		}
	}


即這個繼承了fragment的類裡面的父類的onActivityResult()的實現是在執行父類的也就是Activity中的父類的onActivityResult()後,跳轉到fragment中的這個onActivityResult().說白了,宿主(Activity)中的onActivityResult()只是一個跳轉的中繼點,在fragment中跳轉的是真正的跳轉點,並且在fragment中實現跳轉或獲取資源時前面要加context或向上轉型為FragmentActivity,否則會出現錯誤:該fragment未繫結Activity.


剛開始我的錯誤就是在Activity中沒有寫onActivityResult(),然後一直報錯:沒有繫結activity。只是在fragment中寫了onActivityResult,然後無論怎麼該都沒有進行onActivityResult裡面的程式,也就是斷點進不來,按一般網上說的就是說在Activity裡面用一個fragment的物件,它首先不會實現fragment類裡面的onActivityResult,也就是說會出現空指標錯誤,然後就是寫了在Activity裡面寫了onActivityResult,但是沒有用fragment物件來關聯它,也就是說沒有寫fragmentdemo. onActivityResult();我只是照搬寫了super. onActivityResult();其次,寫好了這個,我們要在fragment類中用引用上下文,也就是與在之前的關聯,建立起真正的完整聯絡,這樣就能實現startActivityforResult與onActivityResult跳轉了。

可能每個人遇到的問題不一樣,我遇到的就是這樣的,如果大家覺得我的方法有用,可以點個贊大笑,如果還有問題,可以交流一下~!