Javacadabra
Javacadabra

Reputation: 5758

Button Background not changing as desired

Does anyone know why this code is not changing the background image of my button as desired?

protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Toast.makeText(getApplicationContext(), "Size: " + Order.getInstance().getOrderItems().size(), Toast.LENGTH_SHORT).show();
    price.setText(String.valueOf(Order.getInstance().getAmount()));


    if(Order.getInstance().getOrderItems().size() > 0 && sent == false){

        sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
        sendBtn.setVisibility(View.VISIBLE);
        sendBtn.setBackgroundResource(R.drawable.send);
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Review Order Screen", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(v.getContext(), ReviewOrderActivity.class);
                sent = true;
                startActivity(i);
            }
        });
    }else if(sent == true){
        sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
        sendBtn.setVisibility(View.VISIBLE);
        sendBtn.setBackgroundResource(R.drawable.pay);
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Pay Order Screen", Toast.LENGTH_SHORT).show();
                //Intent i = new Intent(v.getContext(), PayActivity.class);
                //startActivity(i);
            }
        });
    }

}

If the user presses the button the first time it should set a Boolean to true and start the new Activity. From within the new activity when the user presses a button to return to the previous activity it should now set the image to be different because the Boolean is set to true.

For some reason it is not working...any help is much appreciated!

This is the code within Activity 2 which returns user to previous activity when button pressed:

sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
    sendBtn.setVisibility(View.VISIBLE);
    sendBtn.setBackgroundResource(R.drawable.confirm);
    sendBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Intent k = new Intent(v.getContext(), SectionsActivity.class);
            int tableNum = Order.getInstance().getTableNumber();
            int coverNum = Order.getInstance().getCoverNumber();

            itemNames = new ArrayList<String>();

            //Get dishes from order
            for(int i = 0; i < orderItems.size(); i++){
                MenuItem item = orderItems.get(i);
                String itemName = item.getName();
                itemNames.add(i, itemName);
            }

            RequestTask requestTask = new RequestTask(url, String.valueOf(tableNum), String.valueOf(coverNum), itemNames);
            ReviewOrderActivity.this.finish();
            //startActivity(k);
        }
    });

EDIT TO CODE:

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Toast.makeText(getApplicationContext(), "value: " + sent, Toast.LENGTH_SHORT).show();
    price.setText(String.valueOf(Order.getInstance().getAmount()));


    if(Order.getInstance().getOrderItems().size() > 0 && sent == false){

        sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
        sendBtn.setVisibility(View.VISIBLE);
        sendBtn.setBackgroundResource(R.drawable.send);
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Review Order Screen " + sent, Toast.LENGTH_SHORT).show();
                Intent i = new Intent(v.getContext(), ReviewOrderActivity.class);
                startActivityForResult(i, 0);
            }
        });
    }else if(sent == true){
        sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
        sendBtn.setVisibility(View.VISIBLE);
        sendBtn.setBackgroundResource(R.drawable.pay);
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Pay Order Screen", Toast.LENGTH_SHORT).show();
                //Intent i = new Intent(v.getContext(), PayActivity.class);
                //startActivity(i);
            }
        });
    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

      if (requestCode == 1) {

         if(resultCode == RESULT_OK){      
             sent = true;       
         }
         if (resultCode == RESULT_CANCELED) {    
             sent = false;
         }
      }
    }//onActivityResult

In second Activity

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_review);

    //Get order items from application and assign to list
    orderItems = Order.getInstance().getOrderItems();

    sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
    sendBtn.setVisibility(View.VISIBLE);
    sendBtn.setBackgroundResource(R.drawable.confirm);
    sendBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Intent k = new Intent(v.getContext(), SectionsActivity.class);
            int tableNum = Order.getInstance().getTableNumber();
            int coverNum = Order.getInstance().getCoverNumber();

            itemNames = new ArrayList<String>();

            //Get dishes from order
            for(int i = 0; i < orderItems.size(); i++){
                MenuItem item = orderItems.get(i);
                String itemName = item.getName();
                itemNames.add(i, itemName);
            }

            RequestTask requestTask = new RequestTask(url, String.valueOf(tableNum), String.valueOf(coverNum), itemNames);
            setResult(RESULT_OK); //EDIT

            ReviewOrderActivity.this.finish();
            //startActivity(k);
        }
    });


    //Create adapter and set to the list
    adapter = new ReviewOrderArrayAdapter(this, orderItems);
    this.setListAdapter(adapter);
}

Upvotes: 0

Views: 199

Answers (1)

codeMagic
codeMagic

Reputation: 44571

Since there is no guarantee that Android won't clear out your variables or even your Activity if it needs the memory or that the GC won't nab it up, you need to handle the storage of these variables in onPause(). You can store them in SharedPrefs so that you can retrieve them in onResume()

Another, possibly simpler, option for what you need would be to use StartActivityForResult() and in setResult() of your SecondActivity would be to pass back a RESULT_CODE indicating to change the variable to true. Then you wouldn't need to set it in your FirstActivity

From the Docs

requestCode If >= 0, this code will be returned in onActivityResult() when the activity exits.

you should look for the REQUEST_CODE that you sent. This way you can use startActivityForResult multiple times in the same Activity if you need for different things

Upvotes: 2

Related Questions