gpt4 book ai didi

android - 我们可以在暂停之前在 startActivity() 之后执行代码吗?

转载 作者:行者123 更新时间:2023-11-29 14:54:30 25 4
gpt4 key购买 nike

我的问题是... 如果我们尝试在 startActivity() 之后执行一些代码,我们会在调用当前 Activity 的 onPause() 之前完全执行吗?也就是说,我不会知道当包含它的方法到达末尾时是否真的会调用 startActivity()(finish() 方法发生的事情)。

我有一个示例,其中我想在基于某些条件启动新 Activity 后 detach() 一个对象(具有数据库连接),但我需要此对象来评估一个健康)状况。我知道我可以检查该条件并存储 bool 值和 detach() 它在第一个 if 之前,但我想知道以下代码是否“合法” .

谢谢!

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
School selectedSchool = new School((Cursor)l.getItemAtPosition(position));
mSharedPreferences.edit()
.putLong(DatabaseManager.SCHOOL_ID, selectedSchool.getIdOpenErp())
.commit();
School.SchoolManager schoolManager = new School.SchoolManager(this);
Long[] sessionIdsOpenErpOfSelectedSchool = schoolManager.getSessionIdsOpenErp(selectedSchool);
if (sessionIdsOpenErpOfSelectedSchool.length > 0) {
if (schoolManager.isPreviousWorkingSchoolPresent()) { // line 10
Intent iParticipationManagement = new Intent(this, ParticipationManagement.class);
startActivity(iParticipationManagement);
} else {
Intent iSelectExistingSession = new Intent(this, SelectExistingSession.class);
startActivity(iSelectExistingSession);
}
} else {
Intent iSelectNewSession = new Intent(this, SelectNewSession.class);
startActivity(iSelectNewSession);
}
// The following line will be executed after one of the startActivity() methods is called...
// Is this legal? Or should I get the value from isPreviousWorkingSchoolPresent() (at line 10)
// before the first if and do the detach() there as well?
schoolManager.detach();
}

最佳答案

任何你想在调用 startActivity() 的方法中执行的操作都会在 你收到对 onPause() 的调用之前执行.问题是,您的应用程序默认使用一个主线程,调用 onPause() 和其他生命周期方法发生在它上面。因此,当此线程忙于执行您的方法中的代码时,它无法处理任何其他内容。

如果您的方法在其他线程中执行,这只会是一个问题。然而,情况并非如此,因为此方法用于监听 UI 事件,所以我假设它总是从主线程调用。

关于android - 我们可以在暂停之前在 startActivity() 之后执行代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9619303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com