我彻底阅读了相关主题,但未能找到解决方案。 我有一个FragmentActivity,里面有几个片段。在第二个片段中,我渴望在editText touch上启动活动结果。
edtStreetName.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Intent intent = new Intent(getActivity().getApplicationContext(),GetAddressActivity.class);
startActivityForResult(intent, 1);
return false;
}
});
活动成功启动。经过一些操作后,我想把结果发给片段。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (item.getItemId()) {
case R.id.action_settings:
// Do Activity menu item stuff here
Log.e("menu pressed","menu!");
Intent intent = new Intent();
intent.putExtra("name", "someStreetName");
setResult(RESULT_OK, intent);
finish();
return false;
default:
break;
}
return false;
}
现在是奇怪的事情......活动重新创建并且只在几次重新创建之后 - 结果被发送回片段。有时它可能需要三次或更多次尝试。
Some log cat info:
03-26 00:25:23.634 32242-32242/com.tr.ctprot E/launch ResultActivity﹕ launched
03-26 00:25:23.814 32242-32242/com.tr.ctprot E/launch ResultActivity﹕ launched
03-26 00:25:23.894 32242-32242/com.tr.ctprot E/ResultActivity﹕ created!
03-26 00:25:25.736 32242-32242/com.tr.ctprot E/menu item﹕ is pressed!
03-26 00:25:25.856 32242-32242/com.tr.ctprot E/ResultActivity﹕ created!
03-26 00:25:26.286 32242-32242/com.tr.ctprot E/ResultActivity﹕ destroyed!
03-26 00:25:27.658 32242-32242/com.tr.ctprot E/menu item﹕ is pressed!
03-26 00:25:27.778 32242-32242/com.tr.ctprot E/INTENT Returned﹕ and name is someStreetName
03-26 00:25:27.778 32242-32242/com.tr.ctprot E/INTENT Returned﹕ and name is someStreetName
03-26 00:25:27.778 32242-32242/com.tr.ctprot E/frg﹕ onresumefrags
03-26 00:25:28.168 32242-32242/com.tr.ctprot E/ResultActivity﹕ destroyed!
任何帮助表示赞赏!提前谢谢。
UPD 1 我的片段是onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case (1):
if (resultCode==-1)
Log.e("INTENT Returned "," and name is "+data.getStringExtra("name"));
}
}
Moar logs = - ) 0
3-26 00:25:23.634 32242-32242/com.tr.ctprot E/launch ResultActivity﹕ launched
03-26 00:25:23.814 32242-32242/com.tr.ctprot E/launch ResultActivity﹕ launched
03-26 00:25:23.894 32242-32242/com.tr.ctprot E/ResultActivity﹕ created!
03-26 00:25:23.924 32242-32242/com.tr.ctprot I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
03-26 00:25:23.924 32242-32242/com.tr.ctprot W/dalvikvm﹕ VFY: unable to resolve virtual method 12229: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
03-26 00:25:23.924 32242-32242/com.tr.ctprot D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-26 00:25:23.924 32242-32242/com.tr.ctprot I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
03-26 00:25:23.924 32242-32242/com.tr.ctprot W/dalvikvm﹕ VFY: unable to resolve virtual method 12235: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
03-26 00:25:23.924 32242-32242/com.tr.ctprot D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-26 00:25:23.924 32242-32242/com.tr.ctprot I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
03-26 00:25:23.924 32242-32242/com.tr.ctprot W/dalvikvm﹕ VFY: unable to resolve virtual method 9800: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
03-26 00:25:23.924 32242-32242/com.tr.ctprot D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
03-26 00:25:23.964 32242-32242/com.tr.ctprot I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
03-26 00:25:23.964 32242-32242/com.tr.ctprot W/dalvikvm﹕ VFY: unable to resolve virtual method 12232: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
03-26 00:25:23.964 32242-32242/com.tr.ctprot D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
03-26 00:25:23.964 32242-32242/com.tr.ctprot I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
03-26 00:25:23.964 32242-32242/com.tr.ctprot W/dalvikvm﹕ VFY: unable to resolve virtual method 396: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-26 00:25:23.964 32242-32242/com.tr.ctprot D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-26 00:25:23.964 32242-32242/com.tr.ctprot I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
03-26 00:25:23.964 32242-32242/com.tr.ctprot W/dalvikvm﹕ VFY: unable to resolve virtual method 418: Landroid/content/res/TypedArray;.getType (I)I
03-26 00:25:23.964 32242-32242/com.tr.ctprot D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-26 00:25:23.974 32242-32242/com.tr.ctprot I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
03-26 00:25:23.974 32242-32242/com.tr.ctprot W/dalvikvm﹕ VFY: unable to resolve virtual method 359: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
03-26 00:25:23.974 32242-32242/com.tr.ctprot D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-26 00:25:23.974 32242-32242/com.tr.ctprot I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
03-26 00:25:23.974 32242-32242/com.tr.ctprot W/dalvikvm﹕ VFY: unable to resolve virtual method 361: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
03-26 00:25:23.974 32242-32242/com.tr.ctprot D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-26 00:25:24.034 32242-32246/com.tr.ctprot D/dalvikvm﹕ GC_CONCURRENT freed 217K, 10% free 9661K/10695K, paused 2ms+2ms, total 105ms
03-26 00:25:24.034 32242-32242/com.tr.ctprot D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 38ms
03-26 00:25:24.124 32242-32242/com.tr.ctprot D/AbsListView﹕ Get MotionRecognitionManager
03-26 00:25:24.164 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ Set SWAP INTERVAL 0
03-26 00:25:24.164 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglCreateWindowSurface() surface: 0x4d839120, VC surface: 2, Thread: 32242
03-26 00:25:24.164 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglMakeCurrent(0x4fe14930, 0x4d839120, 0x4d839120) Thread: 32242
03-26 00:25:24.374 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglMakeCurrent(0x4fe14930, 0x4fe149a8, 0x4fe149a8) Thread: 32242
03-26 00:25:24.374 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglMakeCurrent(NULL) Thread: 32242
03-26 00:25:24.374 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglDestroySurface() surface: 0x4fe149a8, android window 0x4fe08d40, Thread: 32242
03-26 00:25:24.454 32242-32242/com.tr.ctprot W/IInputConnectionWrapper﹕ getCursorCapsMode on inactive InputConnection
03-26 00:25:24.464 32242-32242/com.tr.ctprot W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
03-26 00:25:24.464 32242-32242/com.tr.ctprot W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
03-26 00:25:24.464 32242-32242/com.tr.ctprot W/IInputConnectionWrapper﹕ getTextAfterCursor on inactive InputConnection
03-26 00:25:24.635 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglMakeCurrent(0x4fe14930, 0x4d839120, 0x4d839120) Thread: 32242
03-26 00:25:24.645 32242-32242/com.tr.ctprot W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
03-26 00:25:24.655 32242-32242/com.tr.ctprot W/IInputConnectionWrapper﹕ setComposingText on inactive InputConnection
03-26 00:25:25.736 32242-32242/com.tr.ctprot E/menu item﹕ is pressed!
03-26 00:25:25.856 32242-32242/com.tr.ctprot E/ResultActivity﹕ created!
03-26 00:25:25.866 32242-32242/com.tr.ctprot D/AbsListView﹕ Get MotionRecognitionManager
03-26 00:25:25.916 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ Set SWAP INTERVAL 0
03-26 00:25:25.916 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglCreateWindowSurface() surface: 0x4d8abaf8, VC surface: 3, Thread: 32242
03-26 00:25:25.916 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglMakeCurrent(0x4fe14930, 0x4d8abaf8, 0x4d8abaf8) Thread: 32242
03-26 00:25:25.956 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglMakeCurrent(0x4fe14930, 0x4d839120, 0x4d839120) Thread: 32242
03-26 00:25:25.956 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglMakeCurrent(NULL) Thread: 32242
03-26 00:25:25.956 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglDestroySurface() surface: 0x4d839120, android window 0x50b407d8, Thread: 32242
03-26 00:25:25.966 32242-32242/com.tr.ctprot W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
03-26 00:25:25.966 32242-32242/com.tr.ctprot W/IInputConnectionWrapper﹕ setComposingText on inactive InputConnection
03-26 00:25:26.286 32242-32242/com.tr.ctprot E/ResultActivity﹕ destroyed!
03-26 00:25:26.296 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglMakeCurrent(0x4fe14930, 0x4caa4968, 0x4caa4968) Thread: 32242
03-26 00:25:26.306 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglMakeCurrent(NULL) Thread: 32242
03-26 00:25:26.446 32242-32242/com.tr.ctprot D/HAWAII_EGL﹕ eglMakeCurrent(0x4fe14930, 0x4d8abaf8, 0x4d8abaf8) Thread: 32242
03-26 00:25:27.658 32242-32242/com.tr.ctprot E/menu item﹕ is pressed!
03-26 00:25:27.778 32242-32242/com.tr.ctprot E/INTENT Returned﹕ and name is someStreetName
03-26 00:25:27.778 32242-32242/com.tr.ctprot E/INTENT Returned﹕ and name is someStreetName
03-26 00:25:27.778 32242-32242/com.tr.ctprot E/frg﹕ onresumefrags
答案 0 :(得分:0)
我遇到的唯一解决方案是将EditText的TouchListener更改为ClickListener 是:
edtStreetName.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("launch ResultActivity","launched");
Intent intent = new Intent(getActivity().getApplicationContext(),GetAddressActivity.class);
startActivityForResult(intent, 1);
return false;
}
});
成为:
edtStreetName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity().getApplicationContext(),GetAddressActivity.class);
startActivityForResult(intent, 1);
}
});
不知道为什么,但是Activity4Result停止重建自己。