当完成()应该叫什么名字?叫什么名字

2023-09-12 06:01:42 作者:意念成空

我有过一个意向调用次活动的活动。第二个活动被称为后,我想完成的第一项活动。因此,我有以下的code。在的onStop()方法:

  @覆盖
公共无效的onStop(){
    super.onStop();
    如果(shouldBeClosed){
        完();
    }
}
 

现在的问题是,我的第一个活动是没有关闭。我不明白为什么? 也许我应该把这个code到的onPause()? 不过,据我了解在这种情况下,当活动松动焦点(如对话框的呼叫后)我的活动可以被关闭。 所以,问题为什么出现这种情况,我怎么能解决此问题?

P.S。变量 shouldBeClosed 是真实的。这是不是问题的关键。

修改

回马枪 台风 剑鱼 又折返并向海南靠近 未来三天海南依旧 狂风暴雨

下面是第二个活动的号召:

 意向意图=新的意图(这一点,AcSpContextAssign.class);
捆绑额外=新包();
extras.putInt(Constants.KEY_FROM_ACTIVITY,Constants.FROM_AcSpNameCreate);
extras.putLong(Constants.KEY_SPID,SPID);
intent.putExtras(临时演员);
startActivity(意向);
 

之后您发送的意图解决方案

通常情况下,你应该调用finish()为您的第一个活动。是这样的:

  ...
startActivity(secondActivityIntent);
完();
 

这将触发的onPause() - >的onStop() - >的onDestroy()链的第一个活动,所以您可以有进行正常的清理

I have an activity that calls second activity through an intent. After the second Activity is called I want to finish the first activity. Thus, I have the following code in onStop() method:

@Override
public void onStop() {
    super.onStop();
    if (shouldBeClosed) {
        finish();
    }
}

The problem is that my first activity is not closed. And I do not understand why? Maybe I should put this code into onPause()? But as I understand in this case, when activity loose focus (like after the call of a dialog) my activity can be closed. So the question why this happens and how I can correct this behavior?

P.S. The variable shouldBeClosed is true. This is not the point.

EDIT

Here is the call of the second activity:

Intent intent = new Intent(this, AcSpContextAssign.class);
Bundle extras = new Bundle();
extras.putInt(Constants.KEY_FROM_ACTIVITY, Constants.FROM_AcSpNameCreate);
extras.putLong(Constants.KEY_SPID, spId);
intent.putExtras(extras);
startActivity(intent);

解决方案

Normally, you should call finish() for your first activity right after you send an intent. Something like:

...
startActivity(secondActivityIntent);
finish();

This will trigger onPause()->onStop()->onDestroy() chain for your first activity, so you can perform normal clean-up there