开发者

Android - clear history when navigating between Activities

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 lead开发者_StackOverflows 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


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);
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜