什么迫使Android活动在启动另一个活动时被销毁

时间:2016-09-07 09:55:52

标签: java android android-studio in-app-billing

我试图添加应用内购买,当我打电话给OpenIabHelper::launchPurchaseFlow功能时:

  • 我看到Google Play购买窗口
  • onStoponDestroy在我的主要活动中执行而未调用onActivityResult

所以最后我离开了GooglePlay窗口和我的死应用

我尝试通过startActivityForResult启动另一项活动,但效果很好 (没有任何东西被破坏,onActivityResult被调用),所以似乎问题出在OpenIAB library

因此我的问题是:
在致电OpenIabHelper::launchPurchaseFlow后,有什么可能强迫我的活动被破坏?

它似乎不是系统,因为它有足够的内存,正如我所说的那样startActivityForResult正常工作。
此外,没有例外,所以我很困惑,

任何帮助都将受到赞赏

MyActivity.java

public class MyActivity extends AppCompatActivity {
    private OpenIabHelper m_helper;
    private Boolean m_bOpenIabInitialized = false;


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

        OpenIabHelper.Options.Builder builder = new OpenIabHelper.Options.Builder()
            .addAvailableStores(new GooglePlay(this,null))
            .addPreferredStoreName(OpenIabHelper.NAME_GOOGLE)
            .setStoreSearchStrategy(OpenIabHelper.Options.SEARCH_STRATEGY_INSTALLER_THEN_BEST_FIT)
            .setVerifyMode(OpenIabHelper.Options.VERIFY_SKIP)
            .setCheckInventory(false);
        m_helper = new OpenIabHelper(this, builder.build());
        m_helper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                m_bOpenIabInitialized = result.isSuccess();
            }
        });
    }


    public void onListViewItemClicked(String sku, String payload) {
        if (m_bOpenIabInitialized == null || !m_bOpenIabInitialized) return;
        m_helper.launchPurchaseFlow(this, sku, 10001, null, payload);
    }

    @Override
    public void startActivityForResult(Intent intent, int RequestCode) {
        // never reaches here
        Log.d(TAG, "startActivityForResult()");
    }

    @Override
    protected void onActivityResult(int RequestCode, int ResultCode, Intent data) {
        // never reaches here
        Log.d(TAG, "onActivityResult()");
    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example" >

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="com.android.vending.BILLING" />

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:configChanges="orientation|screenSize|keyboardHidden|screenLayout">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

1 个答案:

答案 0 :(得分:1)

您是否尝试过更改活动的launchMode?也许 singleTop 启动模式将保留您的活动:

<activity
    android:name=".MyActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar"
    android:configChanges="orientation|screenSize|keyboardHidden|screenLayout"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

您可以在此处找到有关启动模式的更多信息: https://developer.android.com/guide/topics/manifest/activity-element.html#lmode