android - 为什么 Intent 在 AlertDialog.Builder 之前启动,即使它的编码方式相反

标签 android android-activity android-intent android-alertdialog

如下代码,在调用newPicture时启动Intent,之后显示Dialog。这是什么意思,我该如何更改顺序?

public void newPicture(View v) {
    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    boolean geoProtipAlreadyShown = settings.getBoolean("geoProtipAlreadyShown", false);

    if (!geoProtipAlreadyShown) {
        showGeoProtip();

        // and set the option in SharedPreferences
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("geoProtipAlreadyShown", true);
        editor.commit();
    }

    // start the image capture activity
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(PATH, "tmpfile.jpg")));
    startActivityForResult(intent, IMAGE_CAPTURE);  

}

private void showGeoProtip() {
    String geoProtip = this.getResources().getString(R.string.protip);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(geoProtip).setCancelable(true).setPositiveButton("OK", null);
    AlertDialog alert = builder.create();
    alert.show();
}

最佳答案

将启动图像捕获 Activity 移动到新方法,并将其放入对话框的 OnClickListener:

builder.setMessage(geoProtip).setCancelable(true).setPositiveButton("OK",
    new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            captureImage();
        }
    });


private void captureImage(){
        // start the image capture activity
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(PATH, "tmpfile.jpg")));
        startActivityForResult(intent, IMAGE_CAPTURE);        
}

并修改if-else:

if (!geoProtipAlreadyShown) {
    showGeoProtip();
    ....
}else{
    captureImage();
}

关于android - 为什么 Intent 在 AlertDialog.Builder 之前启动,即使它的编码方式相反,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6150786/

相关文章:

java - 如何将值从父 Activity 传递到子 Activity 以及在子 Activity 退出时刷新父 Activity

android - Bluetooth Intent 的 Bluetooth startActivity 方法

android - 在 Activity 之间转换时偶尔会闪烁

android - 如何将 Html 文本共享到 whatsapp Intent

android - 从通知调用 onNewIntent()

android - 重命名 android 应用程序签名 key 文件

android - 构建 Android 应用程序时禁用 CMake 目标

java - 获取 Facebook 好友性别

android - 在通知中使用 TaskStackBuilder 会重新创建一个 Activity

android - 当使用警报管理器启动应用程序时,如何将额外内容传递给 Intent ?