关于 Activity 生命周期的问题:onNewIntent / onSaveInstanceState

发布于 2024-09-25 16:57:13 字数 1307 浏览 2 评论 0原文

我有 ActivityLanding,它调用一个名为 ActivityFolder 的活动。在我使用 putExtra("folderId", "...") 的意图中(我知道folderId不是最好的例子)

我可以将其存储到一个变量并使用它就很好,但如果最终我的活动被杀死以节省内存我可能会丢失folderId。当我的活动再次启动时, 意图中的“额外”会被保留吗?下面的代码可以正常工作吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.folder);
    onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    String folderId = intent.getStringExtra("folderId"); // Might not be all digits
    // load from server and fill in the blanks
}

如果它确实被保留,那么我可以使用ASyncTask更改它并且它仍然可以正常工作吗?

private class MyAsyncTask extends AsyncTask<String, Object, HashMap<String, Object>> {

.......................................
.......................................
.......................................
    @Override
    protected void onPostExecute(HashMap<String, Object> result) {
        if (this.isCancelled())
            return;
        .....................
        .....................
        .....................
        getIntent().putStringExtra("folderId", ""); // The server wants us to change what folder we are looking at
    }
}

I have ActivityLanding which calls an activity called ActivityFolder. In the intent I used putExtra("folderId", "...") (I know folderId is not the best example)

I can store that to a variable and use it just fine but if eventually my activity gets killed to save memory I may lose the folderId. When my activity gets launched again,, will the
"extras" from the intent get preserved? Will the code below work fine?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.folder);
    onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    String folderId = intent.getStringExtra("folderId"); // Might not be all digits
    // load from server and fill in the blanks
}

If it does get preserved,, then can I change it using ASyncTask and it still work fine?

private class MyAsyncTask extends AsyncTask<String, Object, HashMap<String, Object>> {

.......................................
.......................................
.......................................
    @Override
    protected void onPostExecute(HashMap<String, Object> result) {
        if (this.isCancelled())
            return;
        .....................
        .....................
        .....................
        getIntent().putStringExtra("folderId", ""); // The server wants us to change what folder we are looking at
    }
}

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

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

发布评论

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

评论(1

三生一梦 2024-10-02 16:57:13

当我的活动再次启动时,意图中的“额外内容”会被保留吗?

是的。

When my activity gets launched again,, will the "extras" from the intent get preserved?

Yes.

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