0

When I navigate away from an activity with calculation results,for example to check the diagrams in the help activity and I select the back button in action bar,the calculation have been reset to null.

I know there must be some way of setting the state of the activity to pause instead of having it stopped when I navigate away.

Does anyone know how I would achieve this in code? Is there a way to set the activity so that it doesn't destroy when the user leaves it? Also if I was to set this,would that calculation be fixed to that activity or would it refresh when I recalculate?

The code for the action bar back button is like this:

        final Button actionBarHome = (Button)actionBarLayout.             
        findViewById(R.id.action_bar_title);
        actionBarHome.setBackgroundResource(R.drawable.ic_action_back);
        actionBarHome.setOnClickListener(this);
        actionBarHome.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {                

               startActivity(intent2);

            }

        });

Calculated resut!

Navigated away and clicked back button

Results cleared

Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • Try messing around with the Bundle? – Skynet Dec 18 '13 at 11:08
  • I'm not too familiar with the bundle states,I have just started developing on Android from Java. – Brian Var Dec 18 '13 at 11:11
  • Now is the time to learn: http://developer.android.com/reference/android/os/Bundle.html http://stackoverflow.com/questions/4999991/what-is-the-importance-of-bundle-in-a-android-program http://stackoverflow.com/questions/14876273/simple-example-for-intent-and-bundle – Skynet Dec 18 '13 at 11:13

1 Answers1

3

The activity lifecycle is out of your hands really. You should be handling resuming your activity yourself. Putting your data into the bundle of onSaveInstanceState and using the savedInstanceState Bundle in onCreate to repopulate your views.

You can refer to this article Recreating an Activity

Dazzy_G
  • 820
  • 9
  • 22
  • So what I'm thinking in this situation is: – Brian Var Dec 18 '13 at 11:58
  • `static final String OFFSET_LENGTH = "offsetlength"; public void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putdouble(OFFSET_LENGTH, mark1); super.onSaveInstanceState(savedInstanceState); ` – Brian Var Dec 18 '13 at 12:00
  • And then to recall the data when I load the activity like this inside onCreate? `if (savedInstanceState != null) { // Restore value of members from saved state marl1 = savedInstanceState.getInt(OFFSET_LENGTH);` – Brian Var Dec 18 '13 at 12:00
  • I tried the above,so when I created the `onSaveInstanceState` and I use `.putDouble` it doesn't recognise the variable `mark1` that I want to save from the calculation.Any idea whats wrong here? – Brian Var Dec 18 '13 at 12:09
  • @BrianJ You're using putDouble but getInt? – Dazzy_G Dec 18 '13 at 12:25
  • Okay yes I have changed that,but the mark1,calculation variable is still not being recognised.. – Brian Var Dec 18 '13 at 13:11
  • I'm not sure what you mean when you say that mark1 isn't recognised. You should probably make another question for this issue. – Dazzy_G Dec 18 '13 at 13:32