Activity.finish在某些设备中不起作用?

时间:2019-01-20 17:57:45

标签: android android-activity activity-finish

我在完成活动时遇到了一个奇怪的问题。 我的应用程序中有两个活动。第一个是我的主要活动,将打开第二个活动。第二个活动是带有两个按钮的简单活动。 第一个按钮是“取消”按钮,应该取消该过程并关闭活动。因此,我在单击侦听器中将“取消”按钮称为“取消”按钮。一切都很好,并且在我测试过的所有设备上都可以正常工作。

第二个按钮再次启动另一个活动,等待它完成其工作并返回响应。得到响应后,我调用finish()关闭第二个活动,但是这次在某些设备上不起作用。我跟踪了代码,可以确认在所有经过测试的设备上都调用了finish(),但是正如我所说的,在某些设备中什么也没有发生,并且活动仍然存在。 logcat上没有错误,我完全感到困惑。

如果有人对此有所了解,请与我分享。

谢谢。

已更新

好的,让我这样解释: 我为统一游戏引擎创建了一个IAP插件(由于某些原因,我们使用了自己的购买系统)。当玩家单击“购买”按钮购买某物时,我启动了另一个活动(称为ZarinpalActivity),该活动具有两个按钮(“确认购买并取消”),以及有关玩家尝试购买的产品的一些说明。在ZarinpalActivity上,当玩家单击“取消”按钮时,我调用finish()方法来关闭活动并返回到统一状态。它很简单,并且可以按预期工作。但是,当玩家单击“确认”按钮时,我们创建了一个支付对象,该对象需要一个回调URL(我为我的活动分配了方案和主机作为回调URL)并启动了Web浏览器页面并加载了网关支付页面。现在,在付款页面上,我单击“取消”按钮,因此付款系统将响应重定向到我的应用程序(ZarinpalActivity通过调用回调URL作为响应),因此在onCreate()方法中,我使用getIntent()。getData()得到响应 我处理数据并调用finish()方法,我希望活动将被关闭,它可在90%的设备上运行,但在某些设备上什么也不会发生。 请注意,我可以确认在所有设备上都调用了finish(),我先记录了一条消息,该消息在所有设备上都显示出来。 这是我的ZarinpalActivity:

package com.kingcodestudio.unityzarinpaliab;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.unity3d.player.UnityPlayer;
import com.zarinpal.ewallets.purchase.OnCallbackRequestPaymentListener;
import com.zarinpal.ewallets.purchase.OnCallbackVerificationPaymentListener;
import com.zarinpal.ewallets.purchase.PaymentRequest;
import com.zarinpal.ewallets.purchase.ZarinPal;

public class ZarinpalActivity extends Activity {

    // Unity context.
    private static final String UNITY_GAME_OBJECT_NAME = "ZarinpalAndroid";

    private static String m_merchantID;
    private static String m_callbackScheme;
    private static boolean m_verifyPurchase;
    // Singleton instance.
    public static ZarinpalActivity m_instance;

    //Purchase parameters
    private static long m_amount;
    private static String m_description;
    private static String m_title;


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // If we've received a touch notification that the user has touched
        // outside the app, finish the activity.
        if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
            cancelPurchase();
            return true;
        }

        // Delegate everything else to Activity.
        return super.onTouchEvent(event);
    }

    private void ResizeActivity(){
        WindowManager.LayoutParams params = getWindow().getAttributes();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;
        params.x = -20;
        params.y = -10;
        int orientation = getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            params.height = 3*height/4;
            params.width = 2 * width/3;
        } else {
            params.height = height/2;
            params.width = 3 * width/4;
        }

        this.getWindow().setAttributes(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        m_instance = this;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

        //Returning from payment gateway
        String action = getIntent().getAction();
        Log.d("Zarinpal","Action : "+action);
        if(action!=null && action.equals("android.intent.action.VIEW"))
        {
            Uri data = getIntent().getData();
            if(data==null)
            {
                Log.d("Zarinpal","zarinpal purchase returned null");
            }
            else
            {
                Log.d("Zarinpal","zarinpal purchase returned : "+data.toString());
                String status = data.getQueryParameter("Status");
                if(status!=null && status.equals("OK"))
                {
                    Log.d("Zarinpal","Status OK trying to verify purchase...");
                    String authority = data.getQueryParameter("Authority");
                    UnityPlayer.UnitySendMessage(UNITY_GAME_OBJECT_NAME,"OnPurchaseSucceed",authority);
                    if(m_verifyPurchase){
                        Log.d("Zarinpal","Starting to verify purchase cause autoVerify is set to true : ");
                        verifyPurchase(data);
                    }
                    else
                    {
                        Log.d("Zarinpal","Ignore verifying purchase cause autoVerify is set to false : ");
                        finish();//Finish purchase flow and return o unity
                        return;
                    }
                }
                else
                {
                    try
                    {
                        Log.d("Zarinpal","purchase failed : "+status);
                        UnityPlayer.UnitySendMessage(UNITY_GAME_OBJECT_NAME,"OnPurchaseFailed","null");
                        this.finish();
                        return;
                    }
                    catch(Exception e){
                        Log.d("Zarinpal","Exception : "+e.getMessage());
                    }
                }
            }
        }
        //finish activity because we have no data to handle
        else
        {
            setContentView(R.layout.activity_zarinpal);
            TextView titleTextView = findViewById(R.id.textViewTitle);
            titleTextView.setText(m_title);
            Resources res = getResources();
            String amountCurrencyString = String.format("%,d", m_amount);
            String priceText = res.getString(R.string.priceText, amountCurrencyString);
            TextView priceTextView = findViewById(R.id.priceTextview);
            priceTextView.setText(priceText);
            final Button purchaseButton = findViewById(R.id.purchaseButton);
            final Button cancelButton = findViewById(R.id.cancelButton);
            purchaseButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    cancelButton.setEnabled(false);
                    purchaseButton.setEnabled(false);
                    purchase(m_amount,m_description);
                }
            });

            cancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    cancelPurchase();
                }
            });
        }

        ResizeActivity();
    }

    public static void initialize(String merchantID,boolean verifyPurchase,String callbackScheme){
        m_merchantID = merchantID; // Store merchant id
        m_verifyPurchase = verifyPurchase;//Should verify purchase after purchase completed
        m_callbackScheme = callbackScheme;//App Scheme used for zarinpal callback
        UnityPlayer.UnitySendMessage(UNITY_GAME_OBJECT_NAME,"OnStoreInitialized","store initialized");
    }

    private void cancelPurchase(){
        UnityPlayer.UnitySendMessage(UNITY_GAME_OBJECT_NAME,"OnPurchaseCanceled","cancel by user");
        finish();
    }


    public static void startPurchaseFlow(long amount,String title,String desc)
    {
        m_amount = amount;
        m_description = desc;
        m_title = title;
        Intent intent = new Intent(UnityPlayer.currentActivity,ZarinpalActivity.class);
        UnityPlayer.currentActivity.startActivity(intent);
    }



    private void purchase(long amount, String description){
        EditText emailText = findViewById(R.id.emailText);
        EditText mobileText = findViewById(R.id.mobileText);
        ZarinPal purchase = ZarinPal.getPurchase(UnityPlayer.currentActivity);
        PaymentRequest payment = ZarinPal.getPaymentRequest();
        payment.setMerchantID(m_merchantID);
        payment.setAmount(amount);
        payment.setDescription(description);
        payment.setCallbackURL(m_callbackScheme);
        if(!TextUtils.isEmpty(emailText.getText().toString())){
            payment.setEmail(emailText.getText().toString());
        }
        if(!TextUtils.isEmpty(mobileText.getText().toString())){
            payment.setMobile(mobileText.getText().toString());
        }
        Log.d("Zarinpal","Creating purchase object with callback : "+m_callbackScheme);

        purchase.startPayment(payment, new OnCallbackRequestPaymentListener() {
            @Override
            public void onCallbackResultPaymentRequest(int status, String authority, Uri paymentGatewayUri, Intent intent) {
                if(status==100){
                    Log.d("Zarinpal","Payment started");
                    UnityPlayer.UnitySendMessage(UNITY_GAME_OBJECT_NAME,"OnPurchaseStarted","null");
                    UnityPlayer.currentActivity.startActivity(intent);
                }
                else
                {
                    Log.d("Zarinpal","Payment failed to start");
                    UnityPlayer.UnitySendMessage(UNITY_GAME_OBJECT_NAME,"OnPurchaseFailedToStart","error on payment request");
                }
            }
        });
    }

    private void verifyPurchase(Uri data){
        Log.d("Zarinpal","Verifying purchase for : "+data.toString());
        UnityPlayer.UnitySendMessage(UNITY_GAME_OBJECT_NAME,"OnPaymentVerificationStarted",data.toString());
        ZarinPal.getPurchase(UnityPlayer.currentActivity).verificationPayment(data, new OnCallbackVerificationPaymentListener() {
            @Override
            public void onCallbackResultVerificationPayment(boolean isPaymentSuccess, String refID, PaymentRequest paymentRequest)
            {
                if(isPaymentSuccess)
                {
                    Log.d("Zarinpal","Payment verify success");
                    UnityPlayer.UnitySendMessage(UNITY_GAME_OBJECT_NAME,"OnPaymentVerificationSucceed",refID);
                }
                else {
                    Log.d("Zarinpal","Payment verify failed");
                    UnityPlayer.UnitySendMessage(UNITY_GAME_OBJECT_NAME,"OnPaymentVerificationFailed","purchase is not valid");
                }
                finish();
                return;
            }
        });
    }

    public static void killActivity(){
        m_instance.finish();
    }
}

1 个答案:

答案 0 :(得分:0)

根据您的问题,您假设A,B和C有3个活动。

  1. 活动A 启动活动B
  2. 活动B 中的
  3. 第二个按钮会启动另一个活动(活动C )来完成某些工作。

因此,您必须在完成某些工作后在活动C 中致电finish();,然后将响应返回到活动B
然后在活动B 中处理响应,并致电finish();