0

This app have 2 Activities: MainActivity and SettingActivity (using an intent to send value). ...MainActivity :

String set="1";
public void onCreate(Bundle instant) {
super.onCreate(instant);
setContentView(R.layout.camera);    

...when my app is running, i change value of set : set="2" by SettingActivity , and it work correctly in MainActivity. Then, i press the Home button, and start this app again , it work with set="1" , in layout camera.xml ; And when i press the Back button : after a little change in screen, it work with set="2" with layout camera.xml .

Why it have 2 layout when i run it again after press Home Button. And why i have to Press the Back button to get the exact value of set in that time. i have tried to override onSaveInstanceState (Bundle instant). But it still the same.

Plo_Koon
  • 2,953
  • 3
  • 34
  • 41

2 Answers2

0

Have you try to look at this answer first Saving Android Activity state using Save Instance State

When you press the Home button and you return to your application it can be restarted and so the set variable come back to its original value. To save your variable state you have to write

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putString("set", set);
}

Then you can recover the values like this:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  String myString = savedInstanceState.getString("set");
}

Moreover, why are you using a String to contain integer values?

Hope this help

Community
  • 1
  • 1
garlix
  • 576
  • 9
  • 26
  • i have tried your code , and .... String myString = savedInstanceState.getString("set"); set=myString; ... . then i couldn't change the value of set anymore – Kỳ Đoàn Văn Jan 22 '15 at 16:39
  • Why you can't? You can directly assign to `set` the recovered value like this: `set = savedInstanceState.getString("set");` – garlix Jan 22 '15 at 16:49
  • I don't know why it have 2 layout when i run it again after press Home Button. And why i have to Press the Back button to get the exact value of "set" in that time. But, thank you so much , Garlix :) . – Kỳ Đoàn Văn Jan 22 '15 at 17:05
  • Maybe you can try to post a little bit more of your code. If you find my answer useful please consider to vote it up. You are welcome – garlix Jan 22 '15 at 17:09
  • i couldn't vote it up , because my 'reputation' too low :) . Just thank you :) . – Kỳ Đoàn Văn Jan 23 '15 at 01:25
0
String set="1"; // intial global value for everyone

"when my app is running, i change value of set : set="2" by SettingActivity" by that time MainActivity is paused, you come back, now it is resumed, you press homebutton, then it is paused and stoped, you call it one more time, guess is the os created another activity of it, and started the whole process, you press the back button you kill the newly created class, then the old one pops up, get it? you can always play aroundsingletask or singleTop but why it gave two layouts well it depends on how you called your class or your activity..probably with different intents..

Elltz
  • 10,730
  • 4
  • 31
  • 59