1

在我的应用程序中有 10 个屏幕,我可以从剩余的 9 个屏幕转到屏幕 10,如果我按下设备后退按钮,然后应用程序显示上一个屏幕,但如果屏幕 10 从屏幕 2 重新显示,我会更改路径我如何指定仅此条件。

4

2 回答 2

1

我建议的方式是使用安卓清单。在您要从中跳回的活动节点中,

<activity android:name="com.example.app.ACTIVITY_FROM" >
        <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.app.ACTIVITY_TO_GO_TO" />
</activity>

其中 ACTIVITY_FROM 是您按下后退按钮的活动名称,而 ACTIVITY_TO_GO_TO 是您希望按钮将您带到的活动。在元数据节点中,该android:name部分需要保持原样,这就是说应该将当前活动的父级视为android:value活动。

于 2013-06-20T15:45:42.303 回答
1

最简单的方法可能是在Intent每次用于启动该 Activity 时提供一个独特的额外内容,代表您来自的屏幕,以便您可以根据需要修改行为onBackPressed()

例如,从启动“屏幕 10”的任何 Activity X 中:

Intent intent = new Intent(...);
intent.putExtra("from_screen", x); //where x is a unique number or some other identifier
startActivity(intent);

然后,在目标 Activity 中,您可以检查启动该 ActivityonBackPressed()的额外内容:Intent

@Override
public void onBackPressed() {
    Intent callingIntent = getIntent();
    int callingScreen = callingIntent.getIntExtra("from_screen", -1);

    //Do some logic based on the screen you came from
    switch(callingScreen) {
        case 1:
            //Magic action when we came from screen 1
            break;
        case 5:
            //Magic action when we came from screen 5
            break;
        default:
            //Normal for everyone else, which is to just finish()
            super.onBackPressed();
            break;
    }
}

确保super在您不希望也修改行为的情况下调用。

于 2013-06-20T15:45:52.333 回答