在第一次运行后调用时,finish()不关闭活动

时间:2016-08-19 04:22:37

标签: android android-intent android-activity startactivityforresult

我有三项活动

  • MessagesAttachPhotoActivity
  • MessageGalleryFolderSelectorActivity
  • ImagePickerActivity

MessagesAttachPhotoActivity使用MessageGalleryFolderSelectorActivity调用startActivityForResult()

MessageGalleryFolderSelectorActivity活动会在手机上显示照片文件夹,其中一个会选择一个文件夹。

然后使用ImagePickerActivity调用

setActivityForResult()。从ImagePickerActivity中选择图片后,会通过MessagesAttachPhotoActivity将图片传回MessageGalleryFolderSelectorActivity

当我第一次运行应用程序时,一切正常。但是,如果我之后尝试再次选择图片,MessageGalleryFolderSelectorActivity将在setResult()之后关闭。

我尝试过调用finish()this.finish()((Activity)getApplicationContext()).finish()super.onBackPressed()但未成功。

为什么活动在连续运行时没有关闭?

这是我的代码:

调用MessageGalleryFolderSelectorActivity:

Intent intent;
Bundle arguments = new Bundle();

Bundle bundle;
intent = new Intent(this, MessageGalleryFolderSelectorActivity.class);
bundle = new Bundle();
bundle.putInt(Constants.INTENT_EXTRA_LIMIT, Constants.IMAGES_SELECT_LIMIT);
bundle.putInt("Request", MessageThread.MessageType.IMAGE);
intent.putExtras(bundle);
startActivityForResult(intent, MessageThread.MessageType.IMAGE);

ImagePickerActivity:

imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_MODE,
                      ImagePickerActivity.MODE_MULTIPLE);
imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_LIMIT, 10);
imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_SHOW_CAMERA, false);
imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM,album);

//imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES, images);
startActivityForResult(imagesIntent, MessageThread.MessageType.IMAGE);

将数据传递回MessageGalleryFolderSelectorActivity:

Intent data = new Intent();
data.putParcelableArrayListExtra
     (ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES, selectedImages);               
data.putExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM,album);
setResult(RESULT_OK, data);
finish();
return true;

尝试将数据传递回初始调用活动,但此活动未关闭MessageGalleryFolderSelectorActivity:

@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        ArrayList<Image> selectedImages = data.getParcelableArrayListExtra
            (ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES);

        String album = data.getStringExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM);

        Intent intent = new Intent();
        intent.putExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM, album);
        intent.putParcelableArrayListExtra
          (ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES, selectedImages);

         setResult(Activity.RESULT_OK, intent);

         this.finish();
         return;
    } else if (resultCode == RESULT_CANCELED) {

    }
}

4 个答案:

答案 0 :(得分:2)

我认为我们可以使用getParent()来引用父活动。因此,在MessageGalleryFolderSelectorActivity类中,我们可以在重写的onActivityResult上编写((Activity)getParent())。OnActivityResult(requestCode,resultCode,data)。因此,我们正在将父级的值移交给父级而不进行处理。

答案 1 :(得分:1)

尝试在活动中使用finishAffinity()代替finish()

答案 2 :(得分:0)

工作正常我用3个活动检查过同样的事情: 1. MainActivity 2. SecondActivity 3. ThirdActivity

在MainActivity中,我通过单击按钮启动了SecondActivity,代码如下:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextclick();
            }
        });
    }

    public void nextclick()
    {
        Intent intent=new Intent(MainActivity.this, SecondAcivity.class);
        startActivityForResult(intent,1);
    }



@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode)
        {
            case 1:


                break;
        }
    }

在SecondActivity中我通过单击floatingActionButton启动了ThirdActivity,代码如下:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                nextclick();
            }
        });
    }

    public void nextclick() {
        Intent intent = new Intent(SecondAcivity.this, ThirdActivity.class);
        startActivityForResult(intent, 2);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 2:
                  //Set data for MainActivity
                    Intent intent = new Intent();
                    intent.putExtra("album", "dfdfd");
                    setResult(RESULT_OK, intent);
                    SecondActivity.this.finish();
                    break;
            }
        }
    }

在ThirdActivity中我通过点击floatingActionButton启动了ThirdActivity,代码如下:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
         //Set data for SecondActivity
            Intent data = new Intent();

            data.putExtra("album","album");
            setResult(RESULT_OK, data);
            finish();
        }
    });
}

希望它能帮助您在代码中找到确切的问题。

答案 3 :(得分:-1)

我尝试了与此类

这样的三项活动相同的逻辑

活动A

    public class A extends AppCompatActivity {

    private static final int BCODE = 100;
    private String Tag="A Activity";
    Button triggerButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        triggerButton= (Button) findViewById(R.id.triggerButton);
        triggerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                Bundle arguments = new Bundle();
                intent = new Intent(A.this, B.class);
                intent.putExtras(arguments);
                startActivityForResult(intent, BCODE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==BCODE){
            if(resultCode==RESULT_OK){
                Log.e(Tag,"succes");
            }
        }
    }
}

从活动A开始,我通过使用startActivityForResult

传递意图来启动活动B.

从活动B我再次做同样的事情

public class B extends AppCompatActivity {

    private static final int CCODE =200 ;
    private String Tag="Activity B";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        Intent intent;
        Bundle arguments = new Bundle();
        intent = new Intent(this, C.class);
        intent.putExtras(arguments);
        startActivityForResult(intent, CCODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==CCODE){
            if(resultCode==RESULT_OK){
                Log.e(Tag,"suceess");
                setResult(RESULT_OK,new Intent());
                finish();
            }
        }
    }
}

在活动C中,我刚刚在设置结果后完成活动

public class C extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_c);
        setResult(RESULT_OK,new Intent());
        finish();
    }
}

当活动C结束时,os将恢复活动B并调用onActivityResult()。在活动B的onActivityResult中,我设置结果并完成活动。然后OS将恢复活动B并调用onActivity活动结果AI尝试了很多次这个场景对我来说很好。