简体   繁体   English

Android-重新创建主动性-捆绑包

[英]Android - Recreating an acivity - Bundle

Here is the code for recreating an activity. 这是用于重新创建活动的代码。

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

Here,I have a doubt in the definition of putInt() method of bundle. 在这里,我对bundle的putInt()方法的定义有疑问。 When I looked for its definition, I got the following documentation - 当我寻找它的定义时,我得到了以下文档-

public void putInt (String key, int value) public void putInt(字符串键,int值)
Added in API level 1 在API级别1中添加
Inserts an int value into the mapping of this Bundle, replacing any existing value for the given key. 将int值插入此Bundle的映射中,替换给定键的任何现有值。 Parameters 参量
key: a String, or null key:字符串,或者为null
value: an int, or null 值:一个int或null

I don't understand what is being done with the String key ? 我不明白String键在做什么? I mean to say, is it like that. 我的意思是说,是那样的。 that every time key is used as a pointer to add matter to the bundle ? 每次将键用作将事物添加到捆绑软件中的指针吗? Moreover, is there any need of defining STATE_SCORE as "playerscore" ? 此外,是否需要将STATE_SCORE定义为“ playerscore”?

Nothing will be done to your string key. 您的字符串键不会做任何事情。 It is just like a hash map. 它就像一个哈希图。 I don't quite understand your last question, it is good manner to define the key as a constant. 我不太明白您的最后一个问题,这是将键定义为常量的好方法。

Bundle works like a dictionary. Bundle就像字典一样。 In a dictionary you use a keyword to look for a meaning (or definition). 在字典中,您可以使用关键字来查找含义(或定义)。 That's the purpose of the String . 这就是String的目的。 Bundle use it to retrive the value linked to that key. Bundle使用它来检索链接到该键的值。

Moreover, is there any need of defining STATE_SCORE as "playerscore" 此外,是否需要将STATE_SCORE定义为“玩家得分”

that's not strictly neccessary, but if you declare a constant for your keys, and use always the costant to store/retrieve values from bundle the probability of mispelling decrease near to zero (so more headcache due of intesive debuggin hours) 严格来说,这不是必须的,但是如果您为密钥声明一个常量,并且始终使用代价高昂的方法从捆绑包中存储/检索值,则拼写错误的可能性降低到接近零(由于调试时间过长,导致更多的头缓存)

is there any need of defining STATE_SCORE as "playerscore". 是否需要将STATE_SCORE定义为“ playerscore”。 Ans is NO you can directly give the string Ans不可以直接给字符串

savedInstanceState.putInt("playerscore", mCurrentScore);

I don't understand what is being done with the String key 我不明白String键在做什么

it is like a key value pair. 它就像一个键值对。 when you do this 当你这样做

 savedInstanceState.putInt("playerscore", 1);

the value 1 is stores with the key "playerscore" and when you do a get 值1是存储有键“ playerscore”的字符串,当您获取时

savedInstanceState.getInt("playerscore");

This will return 1 这将返回1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM