在第一次运行后调用Finish()时未关闭活动



我有三个活动

  • MessagesAttachPhotoActivity
  • MessageGalleryFolderSelectorActivity
  • ImagePickerActivity

MessagesAttachPhotoActivitystartActivityForResult()呼叫MessageGalleryFolderSelectorActivity

MessageGalleryFolderSelectorActivity活动显示手机上的照片文件夹,并选择一个文件夹。

然后用setActivityForResult()调用

ImagePickerActivity。一旦从ImagePickerActivity中选择了图像,它将通过MessageGalleryFolderSelectorActivity传递回MessagesAttachPhotoActivity

当我第一次运行应用程序时,一切都很好。但是,如果我尝试之后再次选择图像,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) {
    }
}

我认为我们可以使用getParent()来引用父activity。因此,在MessageGalleryFolderSelectorActivity类中,我们可以在重写的onActivityResult上编写((Activity)getParent() . onActivityResult (requestCode,resultCode,data)。因此,我们将得到的值移交给父值而不处理它

尝试在活动上使用finishAffinity()而不是finish()

它的工作很好,我已经检查了同样的事情与3个活动:1. MainActivity2. SecondActivity3.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();
        }
    });
}

我尝试了同样的逻辑与三个活动像这样

活动

    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");
            }
        }
    }
}

通过startActivityForResult传递intent来启动activity 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中,我正在设置结果并完成活动。然后操作系统将恢复活动B并调用活动a的onActivity结果。我尝试了很多次,这个场景对我来说工作得很好。

相关内容

  • 没有找到相关文章

最新更新