【发布时间】:2012-03-26 00:47:33
【问题描述】:
我的问题是...... 如果我们尝试在 startActivity() 之后执行一些代码,我们会在调用当前 Activity 的 onPause() 之前完全执行吗?也就是说,我没有知道当包含它的方法到达末尾时是否真的会调用 startActivity()(finish() 方法会发生这种情况)。
我有一个示例,我想在基于某些条件启动新 Activity 后 detach() 一个对象(具有数据库连接),但我需要这个对象来评估一个条件。我知道我可以检查该条件并将布尔值和 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();
}
【问题讨论】:
标签: android android-activity onpause