Android example which uses onResume, onStart and onRestart

amit picture amit · Dec 14, 2010 · Viewed 35.9k times · Source

I would like an example Android app which uses onResume, onStart and onRestart.

Answer

Abhishek Susarla picture Abhishek Susarla · Dec 14, 2010
package com.test;

import stuff here

public class Pick_Color extends Activity implements OnClickListener
{

        private Button b11; 
            private Button b12; 
    private Button b13; 
    private Button b14; 

    private Button b_final;

private EditText RED;
private EditText GREEN;
private EditText BLUE;

static int temp_red;
static int temp_green;
static int temp_blue;


public void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.pickcolor);
    Log.d("pick color","on create");
    b11=(Button)findViewById(R.id.buttonR1C1); 
    b12=(Button)findViewById(R.id.buttonR1C2); 
    b13=(Button)findViewById(R.id.buttonR1C3); 
    b14=(Button)findViewById(R.id.buttonR1C4); 

        b11.setBackgroundColor(Color.argb(255, 255, 255,0));
        b12.setBackgroundColor(Color.argb(255, 255, 0,255));
        b13.setBackgroundColor(Color.argb(255, 0, 255,255));
        b14.setBackgroundColor(Color.argb(255, 102, 153,51));

        b11.setOnClickListener(this);
        b12.setOnClickListener(this);
        b13.setOnClickListener(this);
        b14.setOnClickListener(this);



     RED=(EditText) findViewById(R.id.RED_TEXT);
     GREEN=(EditText) findViewById(R.id.GREEN_TEXT);
     BLUE=(EditText) findViewById(R.id.BLUE_TEXT);

       RED.setText(Integer.toString(temp_red));
       GREEN.setText(Integer.toString(temp_green));
       BLUE.setText(Integer.toString(temp_blue));

     b_final=(Button)findViewById(R.id.button_result);
     b_final.setBackgroundColor(Color.argb(255,temp_red,temp_green,temp_blue));
     b_final.setOnClickListener(this);



     OnKeyListener text_listener= new OnKeyListener() 
        {
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                int flag=0;

                try
                {
                    switch(v.getId())
                    {
                        case R.id.RED_TEXT:
                                flag=1;
                            temp_red=Integer.parseInt(RED.getText().toString());

                            if(temp_red<0 || temp_red>255)
                                throw new NumberFormatException();

                        //  Log.d("Pick_Color","temp_red = "+temp_red);

                                            break;

                        case R.id.GREEN_TEXT:
                                flag=2;
                            temp_green=Integer.parseInt(GREEN.getText().toString());

                            if(temp_green<0 || temp_green>255)
                                throw new NumberFormatException();

                        //  Log.d("Pick_Color","temp_green = "+temp_green);

                                            break;

                        case R.id.BLUE_TEXT:
                                flag=3;
                            temp_blue=Integer.parseInt(BLUE.getText().toString());

                            if(temp_blue<0 || temp_blue>255)
                                throw new NumberFormatException();

                        //  Log.d("Pick_Color","temp_blue = "+temp_blue);

                                            break;
                    }
                }
                catch(NumberFormatException n)
                {
                    if(flag==1)
                        {
                        RED.setText("");
                        Log.d("Pick Color","Red Error");
                        }
                    if(flag==2)
                        {
                        GREEN.setText("");
                        Log.d("Pick Color","Green Error");
                        }
                    if(flag==3)
                        {
                        BLUE.setText("");
                        Log.d("Pick Color","Blue Error");
                        }
                }

                Log.d("Pick Color","temp_red = "+temp_red+" temp_green = "+temp_green+" temp_blue = "+temp_blue+"");
                 b_final.setBackgroundColor(Color.argb(255,temp_red, temp_green, temp_blue));
                return false;
            }
        };


            RED.setOnKeyListener(text_listener);
            GREEN.setOnKeyListener(text_listener);
            BLUE.setOnKeyListener(text_listener);

  }

protected void onResume()
{
   super.onResume();

   RED.setText(Integer.toString(temp_red));
   GREEN.setText(Integer.toString(temp_green));
   BLUE.setText(Integer.toString(temp_blue));

   b_final.setBackgroundColor(Color.argb(255,temp_red,temp_green,temp_blue));
}

public void onClick(View v)
{
    switch (v.getId()) 
    {
    // --------------  FIRST ROW --------------
    case R.id.buttonR1C1:

            RED.setText("255");
            GREEN.setText("255");
            BLUE.setText("0");
            temp_red=Integer.parseInt(RED.getText().toString());
            temp_green=Integer.parseInt(GREEN.getText().toString());
            temp_blue=Integer.parseInt(BLUE.getText().toString());
            b_final.setBackgroundColor(Color.argb(255,255,255,0));

            writeColor("Color 255 255 255 0");

        break;

    case R.id.buttonR1C2:

        RED.setText("255");
        GREEN.setText("0");
        BLUE.setText("255");
        temp_red=Integer.parseInt(RED.getText().toString());
        temp_green=Integer.parseInt(GREEN.getText().toString());
        temp_blue=Integer.parseInt(BLUE.getText().toString());
        b_final.setBackgroundColor(Color.argb(255,255,0,255));

            writeColor("Color 255 255 0 255");

        break;

    case R.id.buttonR1C3:

        RED.setText("0");
        GREEN.setText("255");
        BLUE.setText("255");
        temp_red=Integer.parseInt(RED.getText().toString());
        temp_green=Integer.parseInt(GREEN.getText().toString());
        temp_blue=Integer.parseInt(BLUE.getText().toString());
        b_final.setBackgroundColor(Color.argb(255,0,255,255));

            writeColor("Color 255 0 255 255");

        break;

    case R.id.buttonR1C4:

        RED.setText("102");
        GREEN.setText("153");
        BLUE.setText("51");
        temp_red=Integer.parseInt(RED.getText().toString());
        temp_green=Integer.parseInt(GREEN.getText().toString());
        temp_blue=Integer.parseInt(BLUE.getText().toString());
        b_final.setBackgroundColor(Color.argb(255,102,153,51));

            writeColor("Color 255 102 153 51");

        break;


    case R.id.button_result:
        writeColor("Color 255 "+temp_red+" "+temp_green+" "+temp_blue);
        finish();
        break;

    }
}


public void writeColor(String string) 
{
    TouchList.Current_Color=string;
    try 
    {
    TouchList.dos.writeUTF(TouchList.Current_Color);
    } 
    catch (IOException e) 
    {
    e.printStackTrace();
    }
}
}