如何浏览到同一个父状态状态

2023-09-12 01:42:03 作者:じ盛夏花开〃

从我的观察,从Gmail和TED的APP上导航将导航至与父母相同的状态(滚动位置)的bahavior不喜欢谷歌说,在有文档的实施向上导航它想创造一个家长的意图,并启动它。

From my observation from Gmail and TED app the bahavior of up navigation it will navigate to parent with the same state (scroll position) not like what Google say in there doc Implement Up Navigation which like create a parent intent and start it.

我实现了从Android的样品code和所有国家的code都走了(所有多余的参数我有previously设置和滚动位置)。什么是这个正确的方法是什么?我找不到任何在Android上的文件。

I implement the code from Android sample code and all state are gone (All Extra parameters I have previously set and scroll position). What is the proper way on this ? I can't find any on Android document.

下面是code:

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case android.R.id.home:
        Intent upIntent = new Intent(this, MyParentActivity.class);
        if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
            // This activity is not part of the application's task, so create a new task
            // with a synthesized back stack.
            TaskStackBuilder.from(this)
                    .addNextIntent(new Intent(this, MyGreatGrandParentActivity.class))
                    .addNextIntent(new Intent(this, MyGrandParentActivity.class))
                    .addNextIntent(upIntent)
                    .startActivities();
            finish();
        } else {
            // This activity is part of the application's task, so simply
            // navigate up to the hierarchical parent activity.
            NavUtils.navigateUpTo(this, upIntent);
        }
        return true;
}
return super.onOptionsItemSelected(item);

}

在我来说,我得到了3活性说AB和C,当用户从导航到BI把一些额外和BI的的onCreate 使用额外的查询从数据库中的数据填充我的行,当我向后导航从C所有演员都走了,活动B显示了什么。

In my case I got 3 activity say A B and C, when user navigate from A to B I put some extras and onCreate of B I use that extras to query data from database to populate my rows and when I navigate back from C all extras are gone and Activity B show nothing.

推荐答案

标准行为的机器人活动,即创建活动的一个新实例,每次都会有这个活动的一个新的意图(见launchMode-实况这里)。正因为如此你额外似乎消失了,如果你打电话navigateUpTo。

The "standard" behaviour for an android activity is, that a new instance of the activity is created, every time there is a new intent for this activity (see launchMode-docu here). Because of this your extras seem to be gone, if you call navigateUpTo.

在你的情况,我会建议使用

In your case, I would advise to use

android:launchMode="singleTop"

在你的Andr​​oidManifest.xml中的父活动。这样,您将返回到您现有的活动(只要它是你的任务的回堆栈的顶部)。这样你的演员将是preserved。

for your parent activity in your AndroidManifest.xml. This way you will return to your existing activity (as long as it is on the top of the back stack of your task). This way your extras will be preserved.

我也一样,不明白为什么这不是在谷歌文档,你举提到,因为这似乎是一个,如果使用了导航期望​​的行为。

I too, do not understand why this is not mentioned in the Google doc you cite, as this seems the behaviour one expects if using up-navigation.