0

I have button.Onclicking button it cals->choose Browser->on choosing Browser it loads my Url. on coming back from browser,all my previous locally set variable value are cleared.Variables have have values that are initialized with them. for ex:

i=0.

inside method i assign i=10

call browser now // i use this code to call browser

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(strUrl));

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

startActivity(Intent.createChooser(intent, "Choose browser"));

it loads Url//

now on coming back to activity

Varialble (i) value becomes 0.

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96

2 Answers2

1

declare your variable as static

static int i = 0;

this will not reset i to zero. on back press

Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124
1

You need to understand the lifecycle of an Activity a little better. When you leave and return from an Activity state is not automatically persisted.

Android Lifecycle: http://developer.android.com/training/basics/activity-lifecycle/index.html

You should use onSavedInstanceState to restore your state when you return to your Activity. This is the code example the (linked) docs give:

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);
}
Booger
  • 18,579
  • 7
  • 55
  • 72
  • Booger: ya its a better way.Actually i have a class inside a tab with activity group.on back pressed from browser it comes to starting level activity.TabActivty->Act 1(dint call finish())->Activ 2(dint call finish())->browser.on back pressed from browser it goes to TabActivty.Kindly help me with this – Mohamed Hisham Ibn Hanifa Jan 22 '13 at 11:26
  • BUt it works fine with 4.0.3.it works fine with 2.3.3 emulator but create such bug inreal device with os 2.3.5 – Mohamed Hisham Ibn Hanifa Jan 22 '13 at 11:29
  • This is (and always has been) the way to persist state in Android apps (so hasn't changed since the OS was introduced). Maybe you should start a new question with the problem you are now encountering (I think this one was sufficiently answered). – Booger Jan 22 '13 at 11:35