我如何在运行时设置一个活动的父活动?如何在

2023-09-12 02:19:03 作者:小嘴都都o( ̄3 ̄)o

我有一个分层嵌套的看法/活动的任意数目。操作栏应该显示向上导航键导航到任何视图更高的水平。为此,谷歌文档说我必须设置父活性在活动的XML定义的标签。不过,我动态创建我的活动和一个子元素可以是相同的活动,因为它的父。

I have an arbitrary number of hierarchically nested views/activities. The action bar should show the Up navigation button to navigate to a higher level in any view. For this, the google documentation says I have to set the parent activity with a tag in the activity's xml definition. However, I'm creating my activities dynamically and a child element can be of the same activity as it's parent.

因此​​,我怎么设置父活动的实际父实例在运行?

So how do I set the parent activity to the actual parent instance at runtime?

推荐答案

这听起来像你混淆了起来,后退导航。

It sounds like you are confusing up and back navigation.

向上按钮应该是确定性的。从一个给定的屏幕上,向上按钮应该总是使用户在同一屏幕上。

The up button should be deterministic. From a given screen, the up button should always bring the user to the same screen.

后退按钮不应总是带给用户在同一屏幕。返回按钮的目的是帮助用户走回头路通过你的应用程序,按时间顺序。它应该带给用户的previous屏幕。

The back button should not always bring the user to the same screen. The purpose of the back button is to help the user go backwards through your app chronologically. It should bring the user to the previous screen.

如果出现屏幕不清晰的层次结构(例如,没有父/子屏幕),那么你可能不需要实现了导航的。

If there is no clear hierarchy of screens (e.g. there are no parent/child screens), then you may not need to implement up navigation at all.

请参阅:导航与向上向后

一个选项来覆盖默认的向上按钮的行为是简单地拦截了按钮的点击和自己处理。例如:

One option for overriding the default up button behavior is to simply intercept up button clicks and handle it yourself. For example:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        // Launch the correct Activity here
        return true;
    }
    return super.onOptionsItemSelected(item);
}