1

我期待在我目前正在开发的一个小应用程序中出现相当奇怪的行为。

该应用程序包含两个活动。从第一个活动开始,我通过一个意图启动 webbbrowser。当我在浏览器中按下返回键时,即使我在启动之前手动关闭了应用程序,它也会返回到 SECOND 活动。

1) 第一个活动

final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mWebShopURL));
startActivity(intent);

第一个活动像这样启动第二个活动

Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
Bundle b = new Bundle();
b.putString("product", mProduct);
intent.putExtras(b);
startActivity(intent);

2)第二个活动->第一个活动

onBackPressed();

AndroidManifest

    <activity
        android:name=".FirstActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity
        android:name=".SecondActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
    </activity>
4

2 回答 2

0

如果我是正确的你onBackPressed()在你的第二个活动中覆盖。不要这样做将onBackPressed()代码放在评论中并尝试。希望它会有所帮助:)

于 2012-06-13T09:58:47.147 回答
0

如果您希望第二个活动完成并在用户点击您在评论中提到的简单“后退按钮”时消失,那么不要调用onBackPressed(). 请打电话finish()。这将使第二个活动消失并将用户返回到第一个活动。

您的问题是您从未从堆栈中删除第二个活动,这就是从浏览器返回显示该活动的原因。

于 2012-06-13T10:27:44.367 回答