getLastNonConfigurationInstance 始终返回 null

发布于 2024-09-17 12:24:20 字数 389 浏览 2 评论 0原文

HashMap myMap = (HashMap) getLastNonConfigurationInstance();

myMap 始终为空。 getLastNonConfigurationInstance() 返回一个对象。我的地图有两个键“符号”和“名称”。

public Object onRetainNonConfigurationInstance()
    {
        HashMap myMap = new HashMap();
        myMap.put("symbol", this.symbol);
        final Object data = myMap;
        return data;
    }
HashMap myMap = (HashMap) getLastNonConfigurationInstance();

myMap is always null. getLastNonConfigurationInstance() returns an object. My map has two keys "symbol" and "name".

public Object onRetainNonConfigurationInstance()
    {
        HashMap myMap = new HashMap();
        myMap.put("symbol", this.symbol);
        final Object data = myMap;
        return data;
    }

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

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

发布评论

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

评论(4

泼猴你往哪里跑 2024-09-24 12:24:21

我遇到了同样的问题。看起来除了 onCreate() 以外的任何其他调用 getLastNonConfigurationInstance() 都会返回 null。我将该语句移至 onCreate() 方法,瞧……它返回了我期望的结果。

I faced the same issue. Looks like calling getLastNonConfigurationInstance() in anything other than onCreate() returns null. I moved the statement to onCreate() method and voila..it returned what I expected it to return.

久光 2024-09-24 12:24:21

如果 getLastNonConfigurationInstance() 返回一个非 null 对象,则 (HashMap) getLastNonConfigurationInstance() 将返回相同的对象(如果该对象是 HashMap) code>),或者抛出 ClassCastException

您所描述的情况是不可能的,除非您发现了 Java 强制转换运算符中隐藏已久的错误。提示:你还没有。

验证 getLastNonConfigurationInstance() 实际上返回一个非空对象。验证 myMap 实际上为 null。如果您使用调试器来检查这些值,请尝试将它们打印到控制台。调试器有时可能会对您撒谎,或者至少会误导您。

If getLastNonConfigurationInstance() returns a non-null object, then (HashMap) getLastNonConfigurationInstance() will either return the same object (if that object is a HashMap), or throw a ClassCastException.

The situation that you describe is not possible, not unless you've uncovered a long-hidden bug in Java's cast operator. Hint: you haven't.

Verify that getLastNonConfigurationInstance() is actually returning a non-null object. Verify that myMap is actually null. If you're using a debugger to check those values, try printing them to the console instead. Debuggers can lie to you sometimes, or at least mislead.

卖梦商人 2024-09-24 12:24:21

你还没告诉我们什么情况下会发生这种情况吗?当配置发生更改时,onRetainNonConfigurationInstance() 在 Activity 的 onDestroy() 之前调用。

You haven't told us in what situation this happens? onRetainNonConfigurationInstance() is called before an Activity's onDestroy() when a configuration change happens.

梦巷 2024-09-24 12:24:21

getLastNonConfigurationInstance() 将返回保存在 onRetainNonConfigurationInstance() 方法中的引用。

如果发生任何配置更改,将在 onStoponDestroy 之间调用 onRetainNonConfigurationInstance() 方法,在这里您可以保存任何对象引用。

Activity 会保留此引用,直到调用 onResume 为止。

Activity.class

final void performResume(boolean followedByPause, String reason) {
    ...
    mLastNonConfigurationInstances = null;       // clearing reference
    ...
    mInstrumentation.callActivityOnResume(this); // onResume
}

getLastNonConfigurationInstance() will return reference which was saved in onRetainNonConfigurationInstance() method.

onRetainNonConfigurationInstance() method will be called between onStop and onDestroy is case of any configuration change, here you can save any Object reference.

Activity keeps this reference till onResume call.

Activity.class

final void performResume(boolean followedByPause, String reason) {
    ...
    mLastNonConfigurationInstances = null;       // clearing reference
    ...
    mInstrumentation.callActivityOnResume(this); // onResume
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文