0

I need help saving saving the current view of the activity.

What I mean by that is than when the user clicks the back button and then comes back, I want the button that they clicked to remain unchanged and disable the buttons.

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;


public class Question1 extends ActionBarActivity {

Button Pluto, Jupiter, Earth, Mars, Doom;
TextView Points, Dooms;

@Override
final protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question1);

    Pluto = (Button) findViewById(R.id.btnPluto);
    Jupiter = (Button) findViewById(R.id.btnJupiter);
    Earth = (Button) findViewById(R.id.btnEarth);
    Mars = (Button) findViewById(R.id.btnMars);
    Doom = (Button) findViewById(R.id.btnDoom);

    Points = (TextView) findViewById(R.id.txtPoints);
    Dooms = (TextView) findViewById(R.id.txtDooms);

    final Animation FadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
    final Animation FadeOut = AnimationUtils.loadAnimation(this, R.anim.fadeout);

    Pluto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), Checkpoint.class);
            startActivity(i);
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);

            Pluto.startAnimation(FadeIn);
            Pluto.setEnabled(false);
            Pluto.setTextColor(Color.RED);

            Jupiter.startAnimation(FadeIn);
            Jupiter.setEnabled(false);
            Jupiter.setTextColor(Color.GREEN);

            Mars.startAnimation(FadeIn);
            Mars.setEnabled(false);
            Mars.setTextColor(Color.RED);

            Earth.startAnimation(FadeIn);
            Earth.setEnabled(false);
            Earth.setTextColor(Color.RED);

        }
    });
    Jupiter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), Checkpoint.class);
            startActivity(i);
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);

            Jupiter.setEnabled(false);
            Jupiter.setTextColor(Color.GREEN);

            Pluto.startAnimation(FadeIn);
            Pluto.setEnabled(false);
            Pluto.setTextColor(Color.RED);

            Mars.startAnimation(FadeIn);
            Mars.setEnabled(false);
            Mars.setTextColor(Color.RED);

            Earth.startAnimation(FadeIn);
            Earth.setEnabled(false);
            Earth.setTextColor(Color.RED);
        }
    });
    Earth.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), Checkpoint.class);
            startActivity(i);
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);

            Pluto.startAnimation(FadeIn);
            Pluto.setEnabled(false);
            Pluto.setTextColor(Color.RED);

            Jupiter.startAnimation(FadeIn);
            Jupiter.setEnabled(false);
            Jupiter.setTextColor(Color.GREEN);

            Mars.startAnimation(FadeIn);
            Mars.setEnabled(false);
            Mars.setTextColor(Color.RED);

            Earth.startAnimation(FadeIn);
            Earth.setEnabled(false);
            Earth.setTextColor(Color.RED);

        }
    });
    Mars.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), Checkpoint.class);
            startActivity(i);
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);

            Pluto.startAnimation(FadeIn);
            Pluto.setEnabled(false);
            Pluto.setTextColor(Color.RED);

            Jupiter.startAnimation(FadeIn);
            Jupiter.setEnabled(false);
            Jupiter.setTextColor(Color.GREEN);

            Mars.startAnimation(FadeIn);
            Mars.setEnabled(false);
            Mars.setTextColor(Color.RED);

            Earth.startAnimation(FadeIn);
            Earth.setEnabled(false);
            Earth.setTextColor(Color.RED);

        }
    });
    Doom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), Checkpoint.class);
            startActivity(i);
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);

            Pluto.startAnimation(FadeIn);
            Pluto.setEnabled(false);
            Pluto.setTextColor(Color.RED);

            Jupiter.startAnimation(FadeIn);
            Jupiter.setEnabled(false);
            Jupiter.setTextColor(Color.GREEN);

            Mars.startAnimation(FadeIn);
            Mars.setEnabled(false);
            Mars.setTextColor(Color.RED);

            Earth.startAnimation(FadeIn);
            Earth.setEnabled(false);
            Earth.setTextColor(Color.RED);

        }
    });
}


}
  • Did you try to save the view in a static variable on Question1? – Laerte Jan 28 '15 at 20:48
  • possible duplicate of [Saving Activity state in Android](http://stackoverflow.com/questions/151777/saving-activity-state-in-android) – petey Jan 28 '15 at 20:48
  • Have a look at Activity's onSaveInstanceState and onRestoreInstanceState – Michael Krause Jan 28 '15 at 20:49
  • If I understood you correctly your question should call "how to implement view state cache" or something similar. Please check it and rename if it is neccesary. – Alex Zaitsev Jan 28 '15 at 20:56

1 Answers1

0

The main idea is to have some class (let's call it GalacticCache). It has fields you need to save. In can be such:

public class PlanetCache {
    public int planetId;
    public int earthColor;
    public boolean isEarthVisible;
}

public class GalacticCache {
    public List<PlanetCache> planetsState;
}

Then create some CacheManager that will store this GalacticCache (in SharedPreferences or database). When user clics back button - store your model. When your activity is loading - load your model with CacheManager. If your model is empty (app is starting for the first time) - fill it with default data. After loading just set buttons settings according to GalacticCache.

Alex Zaitsev
  • 2,013
  • 4
  • 30
  • 56
  • @AngelAcosta it depends on your needs. Read this: http://developer.android.com/intl/ru/reference/android/content/SharedPreferences.html or http://developer.android.com/intl/ru/training/basics/data-storage/databases.html – Alex Zaitsev Jan 29 '15 at 07:10