user828948
user828948

Reputation: 151

how to reload the activity without refreshing?

i switch to next screen and then comes back to the original and wants to pick up where i left off , save and restore data.In Activity 1:i have more than 10 buttons i can select and unselect button,if go to next screen and if i come back it should not reload,it should show me where i leftoff,

    up1 = (Button) findViewById(R.id.adultup1);
    up1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            if (upt1 == 0) {
                up1.setBackgroundResource(R.drawable.adultup1);
                upt1 = 1;
            } else {
                up1.setBackgroundResource(R.drawable.adultup1_pressed);
                upt1 = 0;
            }
        }
    });

Upvotes: 1

Views: 2758

Answers (1)

Marek Sebera
Marek Sebera

Reputation: 40621

Look at image (and read text too) here: http://developer.android.com/reference/android/app/Activity.html

If you need to not reload state, you should think about what are you doing in each activity state.

If you do:

Activity1 -> startActivity(Activity2) -> Activity2 -> Activity2.onBackPressed -> Activity1

than, there is no reloading. On return from second to first you got called onResume

but If you do:

Activity1 -> startActivity(Activity2) -> Activity2 -> startActivity(Activity1) -> Activity1

then you need to save your state in some external class (static class member) and load Activity1 state from it in onCreate state.
The same would be (as you can see from image), when app proccess is killed, and user returns to interrupted activity.

If you simply return to Activity1 using finish() on Activity2, there will be no call to onCreate, just onResume

enter image description here


Store Button state

public static int _state = -1;

onCreate(Bundle savedInstanceState){
    if(_state == -1){
        _state = 0;
        // this is first time we're using "_state" variable, so do init
    } else {
        // this is second or later, so just load state variable and setup UI
    }
}

Upvotes: 4

Related Questions