finishActivity (int RequestCode) doesn't work

edu*_*213 5 android android-activity

I'm trying to close an Activity that is open from my own activity. I think finish activity is the better way but it doesn't work. Here is my code to open the activity. (Works with a third party package, this may be the problem?)

Intent calendar = new Intent();
calendar.setPackage("com.digibites.calendar");
if (calendar.resolveActivity(getPackageManager()) != null) {
    startActivityForResult(calendar, REQUEST_CALENDAR);
    CURRENT_ACTIVITY = REQUEST_CALENDAR;
}
Run Code Online (Sandbox Code Playgroud)

I close the activity with this, after launch it:

finishActivity(CURRENT_ACTIVITY);
Run Code Online (Sandbox Code Playgroud)

When I start the activity I'm calling a third part package to show a calendar. When the user wants, the calendar is closed by pressing a button. finishActivity is in the method of the button. If I call finish, y close my application activity, not the calendar activity.

Ost*_*tan 5

finishActivity(...)的用途与您想象的不同。它适用于当您想要从启动它的活动中完成结果生成活动时。它违反直觉,但文档对此很清楚:

https://developer.android.com/reference/android/app/Activity#finishActivity(int)

在你的场景中你需要做的是:

只需在您调用结果的活动中的 setResult(..., ...) 之后使用 finish() 即可。

val intent = Intent()
intent.putExtras(yourResultsBundle)
setResult(Activity.RESULT_OK, intent)
finish()
Run Code Online (Sandbox Code Playgroud)