Android Studio 当启动另一个Activity时,什么强制Android Activity被销毁

7rfyedvj  于 2023-10-23  发布在  Android
关注(0)|答案(2)|浏览(163)

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

  • 我看到Google Play购买窗口
  • onStoponDestroy在我的主Activity中执行,而不调用onActivityResult

所以最后我离开了GooglePlay窗口和我的死应用程序
我尝试通过startActivityForResult启动另一个活动,它工作得很好。
(没有任何东西被破坏,onActivityResult被调用),所以问题似乎与OpenIAB library有关

  • 所以我有个问题 *

在调用OpenIabHelper::launchPurchaseFlow后,什么可能会强制销毁我的Activity?
它似乎不是系统,因为它有足够的内存,正如我所说的,
startActivityForResult工作正常。
也没有例外被trown所以我很困惑
任何帮助都将不胜感激

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>
nbnkbykc

nbnkbykc1#

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

<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

5w9g7ksd

5w9g7ksd2#

在我的例子中,我在AndroidManifest.xml中的整理活动下有这个

android:noHistory="true"

删除此选项解决了问题。

相关问题