简体   繁体   English

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

[英]Android - clear history when navigating between Activities

I have 3 Activities that my user continuously is looping through. 我有3个活动,我的用户不断循环。 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? 当用户回到主屏幕时,我需要终止以前的历史记录,这样用户就无法回击按钮并最终进入屏幕#2,那么这样做的好方法是什么? BTW - I'm using 1.6 (API level 4) BTW - 我使用1.6(API级别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 在2.0中,可以覆盖Activity#onBackPressed,但我需要1.6中的类似内容

As I know If you want clear history for activity you can do in two ways 据我所知,如果你想要明确的活动历史,你可以用两种方式做

(1). (1)。 In manifest.xml file 在manifest.xml文件中

You can also implement this from your AndroidManifest.xml file, just adding android:noHistory="true" attribute in those you want 你也可以从你的AndroidManifest.xml文件中实现这个,只需在你想要的那些中添加android:noHistory =“true”属性

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). (2)。 In Java code 在Java代码中

you can add this flag when you start activity Intent.FLAG_ACTIVITY_NO_HISTORY 您可以在启动活动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 . 如果寻找android api级别高达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!! 希望它会帮助你!

Ok, I assume you have 3 activities, A, B and C. A is the main screen and user will loop through these 3 pages. 好的,我假设您有3个活动,A,B和C.A是主屏幕,用户将遍历这3个页面。 But when user enter A, the onBackPresed event should be performed as exit. 但是当用户输入A时,onBackPresed事件应该作为退出执行。 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. 在这种情况下,当您尝试从B或C启动A时,可以将Intent.FLAG_ACTIVITY_CLEAR_TOP添加到Intent,然后清除历史堆栈,堆栈中只有A。

If you want to intercept the back key event, you do not need to override onBackPressed(). 如果要拦截后退键事件,则不需要覆盖onBackPressed()。 We always use onKeyDown before this method is available. 在此方法可用之前,我们总是使用onKeyDown。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM