我的对话框是一个 android:theme="@android:style/Theme.Holo.Dialog" 主题的 Activity ,在 AndroidManifest.xml 中定义。当我从 Activity A 开始 Activity 时:

Intent myIntent = new Intent(this, MyActivityAsDialog.class);
startActivityForResult(myIntent, REQUEST_DIALOG_ID);

打电话后我曾预料到:
setResult(Activity.RESULT_OK);
finish();

MyActivityAsDialog 中会触发 onActivityResult() 上的 Activity A ,我开始 Activity 的地方。

android - Activity startActivityForResult 不会触发 onActivityResult-LMLPHP

问题是,MyActivityAsDialog 正确启动,它正确完成,但是 onActivityResult 中的 Activity A 永远不会被调用。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("EditImageActivity:", "onActivityResult............");
    //code to handle return values comes here
}

AndroidManifest.xml:
<activity
        android:name="my.package.MyActivityAsDialog"
        android:label="@string/title_activity_choose_color_dialog"
        android:theme="@android:style/Theme.Holo.Dialog"
        android:parentActivityName="my.package.ActivityA" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="my.package.ActivityA" />
</activity>

MyActivityAsDialog:
public class MyActivityAsDialog extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_choose_color_dialog);
        setTitle(R.string.colors_content_description);
        //and others setup stuff
    }

    //from layout.xml
    public void cancel(View view) {
         setResult(Activity.RESULT_OK);
         finish();
    }
}

myActivityAsDialog 布局(activity_my_activity_as_dialog.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".ChooseColorDialogActivity" >

     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="cancel"
        android:text="@android:string/cancel" />

</LinearLayout>

Activity A:
public class ActivityA extends Activity {

    public static final int REQUEST_COLOR_DIALOG = 0xDAFFDEBB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_a);
        //and other setup stuff
    }

    //from layout.xml
    public void selectColor(View view) {
        Intent myIntent = new Intent(this, MyActivityAsDialog.class);
        startActivityForResult(myIntent, REQUEST_COLOR_DIALOG);
    }

}

最佳答案

在 MyActivityAsDialog.class 的 xml 文件中,在最上面的 View 中添加以下行

tools:context=".ActivityA"

关于android - Activity startActivityForResult 不会触发 onActivityResult,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40085272/

10-12 03:03