我们可以在startActivity()之后执行代码,然后再暂停它吗? [英] Can we execute code after startActivity() before it gets paused?

查看:429
本文介绍了我们可以在startActivity()之后执行代码,然后再暂停它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是... 如果我们尝试在startActivity()之后执行一些代码,是否会在调用当前Activity的onPause()之前完全执行?也就是说,我不会知道startActivity()是否真正在包含它的方法结束时被调用(finish()方法会发生这种情况).

My question is... If we try to execute some code after startActivity() will it we fully executed before the onPause() of the current Activity is called? That is, I do not know if startActivity() will actually be called when the method that contains it reaches the end (something that happens with the finish() method).

我有一个示例,其中我想根据某些条件在新的Activity启动之后detach()一个对象(具有数据库连接),但是我需要该对象来评估一个条件.我知道我可以检查该条件,并在第一个if之前存储布尔值和detach(),但是我想知道以下代码是否合法".

I have an example in which I want to detach() an object (that has a Database connection) after a new Activity is started based on some conditions, but I need this object to evaluate one condition. I know I could check that condition and store the boolean value and detach() it before the first if, but I would like to know if the following code is "legal".

谢谢!

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()的调用以及发生在其上的其他生命周期方法.因此,尽管此线程忙于执行您方法中的代码,但无法处理其他任何事情.

Anyhting you want execute in the method with the call to startActivity() will get executed before you receive a call to onPause(). The thing is, your application by default uses one main thread, calls to onPause() and other lifecycle methods happen on it. So while this thread is busy with executing the code in your method, it can't process anything else.

仅当您的方法在其他线程中执行时,这才是问题.但是,情况并非如此,因为此方法用于侦听UI事件,所以我假定总是从主线程调用它.

This would only be a problem if your method were executed in some other thread. This is not the case, however, since this method is used to listen to UI events, so I assume that it is always called from the main thread.

这篇关于我们可以在startActivity()之后执行代码,然后再暂停它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆