Cau*_*ons 2 android onpause android-activity
我的问题是...如果我们尝试在 startActivity() 之后执行一些代码,我们会在当前 Activity 的 onPause() 被调用之前完全执行吗?也就是说,我不知道 startActivity() 是否会在包含它的方法到达末尾时实际调用(该finish()方法会发生这种情况)。
我有一个示例,在该示例中,我想要在detach()基于某些条件启动新 Activity 后创建一个对象(具有数据库连接),但我需要此对象来评估一个条件。我知道我可以检查该条件并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();
}
Run Code Online (Sandbox Code Playgroud)
您希望在调用 的方法中执行的任何内容都startActivity()将在您收到对 的调用之前执行onPause()。问题是,您的应用程序默认使用一个主线程,调用onPause()和其他生命周期方法发生在它上面。因此,当该线程忙于执行您方法中的代码时,它无法处理其他任何事情。
如果您的方法在其他线程中执行,这只会是一个问题。然而事实并非如此,因为这个方法是用来监听 UI 事件的,所以我假设它总是从主线程调用的。
| 归档时间: |
|
| 查看次数: |
5496 次 |
| 最近记录: |