开发者

Android, simply view question about view reset

I have a button in my activity, when you click the button an if statement is ran against its text label;

if(BTN_1.getText()=="firsttext"){
    //do some stuff, then...
    BTN_1.setText("secondtext");
}else if(BTN_1.getText()=="secondtext"){
    //do other stuff, then...
    BTN_1.setText("firsttext");
}

Firstly, if I hit the home开发者_运维百科 button and go back to the desktop and then click back onto my app the view has reset its self; if I press the button and leave it in a state where the text of the button is "secondtext", when I return to my app it says "firsttext", how can I stop the view of my app refreshing its self like this?

Secondly, under my XML layout I have defined the buttons text; android:text="firsttext" But this won't actually match my if statement above, under onCreate of this app I have: BTN_CONNECT.setText("Connect"); But visually the text of the button is exactly the same, why won't it match?

Thanks for reading :)


Where do I start?

  • String compares need to be done with equals(), not ==.
  • String compares to check your state are bad to begin with. Use an integer/enum to see manage your state.
  • Depending on how long you want your state to persist, you can either do it in Activity.saveInstanceState() (so it will persist if you change orientation), or in the SharedPreferences if you want it to persist forever.
  • Don't use hard-coded strings for android:text, use resources, so you can translate them.
  • I don't understand your last point.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜