Android - 在活动之间导航时清除历史记录

发布于 2024-08-11 06:51:55 字数 229 浏览 8 评论 0原文

我的用户不断循环浏览 3 个活动。当用户返回主屏幕时,我需要终止以前的历史记录,这样用户就无法点击后退按钮并最终进入屏幕#2,执行类似操作的好方法是什么? 顺便说一句 - 我正在使用 1.6(API 级别 4)

重申 - 说我不知道​​或预测引导我到达原始视图的路径。但是一旦我加载它,我想清除引导用户访问该视图的历史记录。在 2.0 中可以覆盖 Activity#onBackPressed 但我需要在 1.6 中类似的东西

I have 3 Activities that my user continuously is looping through. When user is back to the main screen I need to terminate previous history so user cannot hit back button and end up on screen #2, what would be a good way to do something like that?
BTW - I'm using 1.6 (API level 4)

To reiterate - say I don't know or predict the path which leads me to the original view. But once I load it I want to clear history that led user to that view. In 2.0 it's possible with overwriting Activity#onBackPressed but I need something like that in 1.6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生漫 2024-08-18 06:51:55

好的,我假设您有 3 个活动,A、B 和 C。A 是主屏幕,用户将循环浏览这 3 个页面。但是当用户进入A时,onBackPresed事件应该作为退出执行。我说清楚了吗?

在这种情况下,当你尝试从B或C启动A时,你可以将Intent.FLAG_ACTIVITY_CLEAR_TOP添加到Intent中,然后历史堆栈将被清除,堆栈中将只有A。

如果要拦截后退键事件,则不需要重写onBackPressed()。在这个方法可用之前我们总是使用onKeyDown。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

    }
    return super.onKeyDown(keyCode, event);
}

Ok, I assume you have 3 activities, A, B and C. A is the main screen and user will loop through these 3 pages. But when user enter A, the onBackPresed event should be performed as exit. Do i make it clear?

In such situation, when you try to start A from B or C, you can add Intent.FLAG_ACTIVITY_CLEAR_TOP to the Intent, then the history stack will be cleared and there will be only A in your stack.

If you want to intercept the back key event, you do not need to override onBackPressed(). We always use onKeyDown before this method is available.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

    }
    return super.onKeyDown(keyCode, event);
}
往昔成烟 2024-08-18 06:51:55

据我所知,如果您想要清晰的活动历史记录,可以通过两种方式进行

(1)。在manifest.xml文件中

您还可以从AndroidManifest.xml文件中实现这一点,只需在您想要的

示例

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.rdc.activity"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="xx" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".PickGalleryImageActivity"
                  android:label="@string/app_name"
                  android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

(2)中添加 android:noHistory="true" 属性即可。在Java代码中,

时添加此标志,

您可以在启动活动Intent.FLAG_ACTIVITY_NO_HISTORY示例

Intent intent = new Intent(this, SomeOtherClass.class);

// do not keep this intent in history
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

startActivity(intent);

另一个是如果您正在寻找后退按钮刺穿,下面的示例可能会帮助您

如果正在寻找androidapi级别高达 1.6。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {
     //todo here
     return true;
     }
     return super.onKeyDown(keyCode, event);    
}

希望它能帮助你!

As I know If you want clear history for activity you can do in two ways

(1). In manifest.xml file

You can also implement this from your AndroidManifest.xml file, just adding android:noHistory="true" attribute in those you want

example

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.rdc.activity"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="xx" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".PickGalleryImageActivity"
                  android:label="@string/app_name"
                  android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

(2). In Java code

you can add this flag when you start activity Intent.FLAG_ACTIVITY_NO_HISTORY

example

Intent intent = new Intent(this, SomeOtherClass.class);

// do not keep this intent in history
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

startActivity(intent);

And the other one is if you are looking for Back Button impalement below example may help you

If looking for android api level upto 1.6.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {
     //todo here
     return true;
     }
     return super.onKeyDown(keyCode, event);    
}

Hope It will help you!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文