2

In my program, I'm creating shapes which are filled with random colors from a list. The colors are set in onCreate. I don't want it to have determined colors, but to change the colors multiple times. How do I 'restart' the onCreate part, so the colors are allocated again?

public class MainActivity extends Activity implements OnGestureListener
{       
            private Paint paint = new Paint();


            @Override
            protected void onCreate(Bundle savedInstanceState)
            {   
                super.onCreate(savedInstanceState);                       
                setContentView(R.layout.activity_main);                           
                Canvas canvas = new Canvas(bg); 

                List<Integer> numbers = Arrays.asList(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW);
                Collections.shuffle(numbers);

            }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
jle
  • 686
  • 1
  • 8
  • 24
  • 1
    When do you want to change the colors? When activity restarts, backs to foreground, click button...? – Tomasz Czura Dec 06 '16 at 11:32
  • Check out these questions: [question1](http://stackoverflow.com/questions/7150014/how-to-restart-the-oncreate-function), [question2](http://stackoverflow.com/questions/6134249/android-activity-restart), may be this is what you are looking for. – Nayana_Das Dec 06 '16 at 11:35
  • I have an animation part which I left out here, when the animation is done the colors should swap again. But if you tell me how I'd do it with a button I dispose it to an animation – jle Dec 06 '16 at 11:37

3 Answers3

2

Here is the trick, call this method

loadColors();

in oncreate and anywhere else u want

public class MainActivity extends Activity implements OnGestureListener
{       
            private Paint paint = new Paint();


            @Override
            protected void onCreate(Bundle savedInstanceState)
            {   
                super.onCreate(savedInstanceState);                       
                setContentView(R.layout.activity_main);                           
                Canvas canvas = new Canvas(bg); 

                loadColors();

            }



         private void loadColors(){
            List<Integer> numbers = Arrays.asList(Color.RED, Color.BLUE,                      
Color.GREEN, Color.YELLOW);
                 Collections.shuffle(numbers);
    }
    }
Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • I'd like to try your code, but I'm creating shapes in onCreate which are refered to `numbers`, so I get errors, if I'll take it out of the onCreate part. Is your way possible then? – jle Dec 06 '16 at 18:41
1

this may help you, just put in your onResume or the method you want to restart oncreate from

onCreate(new Bundle()); 

for example by clicking a button like this

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onCreate(new Bundle()); 
        }
});
masoud vali
  • 1,528
  • 2
  • 18
  • 29
0

If you are trying to change the colors when you navigate from one activity to another then you can probably try wrapping up your code for changing the colors in a function and call that function in onResume() (one of the life cycle methods of an activity). onResume() will be called every time when your activity is loaded.

Itapu Vinay
  • 687
  • 2
  • 9
  • 18