开发者

Question about activity lifecycle: onNewIntent / onSaveInstanceState

开发者 https://www.devze.com 2023-01-18 07:05 出处:网络
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 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;
        ..........开发者_JS百科...........
        .....................
        .....................
        getIntent().putStringExtra("folderId", ""); // The server wants us to change what folder we are looking at
    }
}


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

Yes.

0

精彩评论

暂无评论...
验证码 换一张
取 消