关于 Activity 生命周期的问题:onNewIntent / onSaveInstanceState
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。
Yes.