简体   繁体   中英

Android Paypal launch button OnClick is executed only once in the activity lifespan

This one is destroying my brain, when you enter the activity you can click the paypal button and you go into the paypal activity fine. If you cancel the paypal activity you are brought back to the original activity. From here if you click the button again nothing happens. I tried printing a basic message to console in the OnClick method and it didn't even show, it doesn't seem to be registering the clicks. I'm Assuming the problem has nothing to do with the layout xml file or the manifest file. I would post an image but i cant because im new, all you need to know is that its a checkout screen with a paypal button really.

Thanks in advance, Hugh.

package com.cit.datastructuretesting;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.paypal.android.MEP.CheckoutButton;
import com.paypal.android.MEP.PayPal;
import com.paypal.android.MEP.PayPalPayment;

public class StoreItemInterface extends Activity implements OnClickListener, OnKeyListener
{           
Double subtotal, shipping, total;
EditText etItemQuantity;
TextView tvItemSubtotal, tvItemTotal;
DecimalFormat decFormat = new DecimalFormat("#.##");
PayPal ppObj;
CheckoutButton launchPayPalButton;



@Override
protected void onCreate(Bundle savedInstanceState) 
{       
    super.onCreate(savedInstanceState);     

    setContentView(R.layout.storeiteminterface);

    ImageView ivItemImage = (ImageView) findViewById(R.id.ivItemImage2);
    TextView tvItemTitle = (TextView) findViewById(R.id.tvItemTitle2);
    TextView tvItemDescription = (TextView) findViewById(R.id.tvItemDescription2);
    etItemQuantity = (EditText) findViewById(R.id.etItemQuantity);
    tvItemSubtotal = (TextView) findViewById(R.id.tvItemSubtotal);
    TextView tvItemShipping = (TextView) findViewById(R.id.tvItemShipping);
    tvItemTotal = (TextView) findViewById(R.id.tvItemTotal);    

    subtotal = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getPrice() * Integer.parseInt(etItemQuantity.getText().toString());
    shipping = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getShippingPrice();
    total = subtotal + shipping;

    ivItemImage.setImageBitmap(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getImageBitmap());            
    tvItemTitle.setText(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getTitle());
    tvItemDescription.setText(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getDescription());
    tvItemSubtotal.setText("€" + decFormat.format(subtotal));       
    tvItemShipping.setText("€" + decFormat.format(shipping));       
    tvItemTotal.setText("€" + decFormat.format(total)); 

    etItemQuantity.setOnKeyListener(this);

    //setup paypal stuff
    ppObj = PayPal.initWithAppID(this.getBaseContext(), "APP-80W284485P519543T", PayPal.ENV_SANDBOX);

    launchPayPalButton = ppObj.getCheckoutButton(this, PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.bottomMargin = 10;

    launchPayPalButton.setLayoutParams(params);        

    ((RelativeLayout)findViewById(R.id.paypalLayout)).addView(launchPayPalButton); 

    launchPayPalButton.setOnClickListener(this);
}

@Override
public void onClick(View arg0) 
{       
    if(arg0.getId() == launchPayPalButton.getId())
    {
        System.out.println("TEST");

        if(!tvItemTotal.getText().toString().equals("---"))
        {
            PayPalPayment newPayment = new PayPalPayment();
            newPayment.setSubtotal(BigDecimal.valueOf(total));
            newPayment.setCurrencyType("EUR");
            newPayment.setRecipient("Cape_1328492032_biz@mycit.ie");
            newPayment.setMerchantName("Cape Clear App");

            Intent paypalIntent = PayPal.getInstance().checkout(newPayment, StoreItemInterface.this);
            StoreItemInterface.this.startActivityForResult(paypalIntent, 1);
        }
        else
        {
            Toast.makeText(StoreItemInterface.this, "Invalid Quantity!", Toast.LENGTH_SHORT).show();
        }
    }       
}

@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) 
{       
    if(etItemQuantity.getText().toString() != null && !etItemQuantity.getText().toString().trim().equals("") && !etItemQuantity.getText().toString().trim().equals("0"))
    {
        subtotal = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getPrice() * Integer.parseInt(etItemQuantity.getText().toString());
        total = subtotal + shipping;

        tvItemSubtotal.setText("€" + subtotal);         
        tvItemTotal.setText("€" + decFormat.format(total));
    }
    else
    {
        tvItemSubtotal.setText("---");          
        tvItemTotal.setText("---");         
    }

    return false;
}   

}

In case you are still looking for an answer, I have one

If you have a look at getCheckoutButton method, it takes Context as parameter, so when the Activity is for eg say Paused which happens when you start another Activity , the instance of the CheckoutButton is lost somehow.

I fixed is by using updateButton method in onResume of the Activity

@Override
protected void onResume() {
    /**
     * The CheckoutButton has to be updated each time the Activity is
     * resumed, otherwise the onClickListener of CheckoutButton will not work
     **/
    if (mCheckOutBtn != null && (mCheckOutBtn instanceof CheckoutButton))
        mCheckOutBtn.updateButton();
    super.onResume();
}

This works considering you initialized PayPal library and CheckoutButton in onCreate of Activity .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM