简体   繁体   English

在Android活动上调用finish()实际上并没有完成

[英]Calling finish() on an Android activity doesn't actually finish

I'm calling finish() but my activity keeps on going. 我正在调用finish()但我的活动仍在继续。

I have an activity which is invoked by a menu from the main activity screen. 我有一个活动,由主活动屏幕中的菜单调用。 In my activity's onCreate() method I have the following code fragment: 在我的activity的onCreate()方法中,我有以下代码片段:

    // Make sure there are some events in the list.
    if (theEventArrayList.isEmpty()){
        Toast.makeText(this, "Event List is empty", Toast.LENGTH_LONG).show();
        finish();
    }
    SummarizeCurrentEvent();
    graphEvents();

If the list is empty it puts up the Toast, and I can set breakpoint on the call to finish() . 如果列表为空,则会显示Toast,我可以在调用finish()设置断点。 If I step from that in the debugger it goes to straight to SummarizeCurrentEvent() . 如果我从调试器中的那个步骤开始,它将直接转到SummarizeCurrentEvent() I thought finish() would exit the activity. 我以为finish()会退出活动。 Is this not the case? 这不是这种情况吗? Where can I find out more information about this method? 我在哪里可以找到有关此方法的更多信息?

You should put a return statement after that finish , because the method that called finish will be executed completely otherwise. 你应该在finish之后放置一个return语句,因为调用finish的方法将完全执行。

also, see this question: about finish() in android 另外,请看这个问题: 关于android中的finish()

finish() just tells the activity to do what it needs to do to finish, eg. finish()只是告诉活动做它需要完成的事情,例如。 shutdown, call on onPause, report result to parent, etc. It doesn't do an exit() call or anything. 关闭,调用onPause,向父级报告结果等。它不执行exit()调用或其他任何操作。

You should return after the finish() call. 你应该在finish()调用后返回。

Adding to the other answers, you still may have (Re)onStart , onResume and onPause invoked. 添加到其他答案,您仍然可以调用(Re)onStartonResumeonPause

I say this because in the following link, there is a table that says that for one activity to be killed, first it is invoked onPause (and probably but not guaranteed) on Stop and onDestroy. 我之所以这么说,是因为在下面的链接中,有一个表格表示对于一个要杀死的活动,首先在Stop和onDestroy上调用onPause(可能但不保证)。

Reference Activity 参考活动

This is the case where the try...catch statement should be used. 这是应该使用try...catch语句的情况。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        //...some initialization...

        // Make sure there are some events in the list.
        if (theEventArrayList.isEmpty()){
            throw new Exception("Event List is empty");
        }
        SummarizeCurrentEvent();
        graphEvents();
    } catch (Exception e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        finish();
    }
}

Finish finishes the activity, but it's up to the main loop to do any UI interaction. Finish完成活动,但是由主循环完成任何UI交互。 You have to wait until the UI loop runs, which is after you return from onCreate . 你必须等到UI循环运行,这是从onCreate返回之后。

put in manifest : 放入清单:

    <activity android:name=".MainActivity"
        android:noHistory="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

to avoid holding it in history stack of the system 避免将其保留在系统的历史堆栈中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM