如何获取当前的activte活动?

发布于 2024-11-02 07:53:10 字数 262 浏览 3 评论 0原文

我的应用程序有多项活动。我需要确定其中哪一个当前处于活动状态(或者无论术语是什么,活动、前景、显示……)。有办法做到这一点吗?

(本以为这是一个简单又常用的东西,但是找了半个小时,什么也没得到。:-(

(RunningTaskInfo.topActivity() 只返回 ComponentName,但我需要它的引用,所以这个不帮助。)

(一个强力的解决方案是在全局变量中跟踪活动的引用。但这必须在每个 onResume() 中完成。在我看来,这很丑陋。)

My app has several activities. I need to determine which one of them is currently active (or whatever the term is, active, foreground, showing, ...). Is there a way to do this?

(I thought this is an easy and commonly-used thing, but after searching for half and hour, I got nothing. :-(

(RunningTaskInfo.topActivity() only returns the ComponentName, but I need its reference, so this doesn't help.)

(A brute-force solution is to keep track of the activity's reference in a global variable. But this has to be done in each and every onResume(). It's ugly IMO.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯角 2024-11-09 07:53:10

有办法做到这一点吗?

并不真地。

(强力解决方案是在全局变量中跟踪活动的引用。但这必须在每个 onResume() 中完成。在我看来,这很丑陋。)

这也是内存泄漏的一个原因。

就我个人而言,我会找到一种更好的方法来解决您认为通过获取“当前活动[原文如此]活动”来解决的任何问题。

Is there a way to do this?

Not really.

(A brute-force solution is to keep track of the activity's reference in a global variable. But this has to be done in each and every onResume(). It's ugly IMO.)

It's also a recipe for memory leaks.

Personally, I'd find a better way to solve whatever problem you think your are solving by getting the "currently activte [sic] activity".

扛刀软妹 2024-11-09 07:53:10

我几乎不是专家,但我需要做一些像您提到的保持当前突出显示的按钮突出显示(底部栏)的事情。我想我在每个活动中都使用了 onResume() ,正如您提到的将其存储在应用程序类变量中。我确信有更好的方法,但

编辑:实际上我只是重新阅读了您提出的内容...是的,我同意公共软件存储对活动的实际引用是不好的。为什么不能只存储当前活动名称的字符串变量?这就是我所做的来跟踪并突出显示适当的按钮

I'm not nearly an expert but i needed to do something like you mention keep a current highlighted button highlighted (bottom bar). I think i used onResume() in each activity as you mention to store it in a application class variable. I'm sure there is a better way though

Edit: actually i just re-read what you proposed... yeah i agree with commonsware storing an actual reference to the activity is bad. Why can't you just store a string variable of the name of the current activity? thats what i did to keep track and highlight an appropriate button

謌踐踏愛綪 2024-11-09 07:53:10

无论谁遇到过这个问题,即您需要当前活动活动的引用,这里没有真正的解决方案。唯一的方法是你自己使用全局来跟踪它。是的,使用全局确实不是Android编程的最佳方式,但我认为它的成本更低。

这就是我所做的:

从同一个基本活动扩展我的所有活动:MotherOfAllActivities。

public class MotherOfAllActivities extends Activity {
    public void onCreate(...) {
        globalCurrentActivity = this;
        ........
    }

    public void onResume() {
        globalCurrentActivity = this;
        ........
    }

    public void onPause() {
        if (globalCurrentActivity == this)
            globalCurrentActivity = null;
    }

    // And, don't forget about onActivityResult(). It can come before onResume()
    // of the calling activity and after onPause() of the called activity. That is,
    // there is a window when globalCurrentActivity is null.
    public void onActivityResult() {
        globalCurrentActivity = this;
    }
}

如果您不喜欢这个答案,请不要开枪打我。我只是想与人们分享对我有用的东西,至少到目前为止是这样。

Whoever came across this problem, i.e., you need a reference to the current active activity, there is no a real solution here. The only way is you keep track of it by yourself, using a global. Yes, using a global is really not the best way for Android programming, but I think it's less costly.

Here is what I did:

Extend all my acitivities from the same base activity: MotherOfAllActivities.

public class MotherOfAllActivities extends Activity {
    public void onCreate(...) {
        globalCurrentActivity = this;
        ........
    }

    public void onResume() {
        globalCurrentActivity = this;
        ........
    }

    public void onPause() {
        if (globalCurrentActivity == this)
            globalCurrentActivity = null;
    }

    // And, don't forget about onActivityResult(). It can come before onResume()
    // of the calling activity and after onPause() of the called activity. That is,
    // there is a window when globalCurrentActivity is null.
    public void onActivityResult() {
        globalCurrentActivity = this;
    }
}

Don't shoot me if you don't like this answer. I'm just trying to share with people of what works for me, at least so far.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文