点击后退按钮但不终止活动并让它进入后退堆栈(Android)

发布于 2024-12-25 07:12:55 字数 1046 浏览 0 评论 0原文

我有 3 个活动 A、B 和 C。

我通过单击按钮动态地扩展活动 B,用户可以添加任意数量的视图。

操作是这样的:

用户看到“活动 A”首先输入他的详细信息并单击“保存”按钮,我将他带到“活动 B”,在那里他可以多次添加某些字段,当他再次单击“保存”时,我将他带到“活动 B” “活动C”。

现在,当我在 A 处转到 B 并添加一些视图并在 TextViews 中输入文本,然后单击“保存”并转到 C 时。从 C 开始,如果我回击,我会看到 B 完好无损,以及所有膨胀的视图并输入文本,显然是因为它保存在返回堆栈中,但是如果我通过回击从 B 转到 A,然后返回到 B,则所有视图都会消失,因为它已从返回堆栈中删除。

我想知道是否有可能在 BackStack 中只保存一个 B 实例,并且当用户回击时根本不杀死它? 我已经覆盖了后退键,但无济于事,因为活动无论如何都会被终止,有些人建议我应该将整个视图和数据保存到 Parcelable ArrayList 中,并在 onCreate 中重新生成它们,但这对我来说似乎不可行,因为我认为无论如何我们可以将它保存在 BackStack 中。

我在 Android 开发人员 http://developer .android.com/guide/topics/fundamentals/tasks-and-back-stack.html 说明了这些活动属性

android:taskAffinity
android:launchMode
android:allowTaskReparenting
android:clearTaskOnLaunch
android:alwaysRetainTaskState
android:finishOnTaskLaunch
android:singleTask
android:singleInstance

,但我不知道如何使用它们。

有人尝试过这个吗?如果是这样,请帮我把这些碎片拼凑起来。

I have 3 activities A, B and C.

I inflate activity B dynamically on a button click and the user can add as many views as he likes.

The operation is like this:

User sees "Activity A" first enters his details and clicks the save button and I take him to "Activity B" where he adds certain fields as many times he likes and when he clicks save again I take him to "Activity C".

Now when I am at A and go to B and add some views and enter text in TextViews and then hit save and go to C. From C if I hit back I see B intact along with all the inflated views and entered text obviously because it is saved in the Back Stack, but if I go from B to A by hitting back and then come back to B all the views are gone because it is removed from the Back Stack.

I wanted to know if it is possible to hold only one instance of B in the BackStack and not kill it at all when the user hits back?
I had overriden back key but to no avail because the activity is killed anyways, some people suggested that I should save the entire views and data from it to a Parcelable ArrayList and regenerate them again in onCreate but that doesnt look feasible to me as I think we can save it in BackStack anyway.

I came across this guide at android developers http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html which says about these activity attributes

android:taskAffinity
android:launchMode
android:allowTaskReparenting
android:clearTaskOnLaunch
android:alwaysRetainTaskState
android:finishOnTaskLaunch
android:singleTask
android:singleInstance

But I am unaware of how to put them to use.

Has anyone tried this out yet? If so please help me put the pieces together.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2025-01-01 07:12:55

是的,我同意南迪什的观点。

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
            switch(keyCode)
            {
            case KeyEvent.KEYCODE_BACK:
                //bring back the previous activity do your logic here           
                return false; //means you don't want to remove the activity from stack 
            }
        return super.onKeyDown(keyCode, event);  // means u want to remove the last activity from Activity stack.
    }

    so question  is that how u can go to other activity without remove it from stack, 
    you can use  :
                Intent myIntent = new Intent(CurrentClass.this, JumptoActivity.class);
                 startActivity(myIntent);*

example: at the switch case u can use this

if(KeyEvent.KEYCODE_BACK)
{
                Intent myIntent = new Intent(CurrentClass.this, NextActivity.class);
                 startActivity(myIntent);
                return false;
}
else
    return true;       //if you not write this then your  menu and other think will be affected.

谢谢您,我认为这一点信息会对您有所帮助。

Yes,I agree with Nandeesh.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
            switch(keyCode)
            {
            case KeyEvent.KEYCODE_BACK:
                //bring back the previous activity do your logic here           
                return false; //means you don't want to remove the activity from stack 
            }
        return super.onKeyDown(keyCode, event);  // means u want to remove the last activity from Activity stack.
    }

    so question  is that how u can go to other activity without remove it from stack, 
    you can use  :
                Intent myIntent = new Intent(CurrentClass.this, JumptoActivity.class);
                 startActivity(myIntent);*

example: at the switch case u can use this

if(KeyEvent.KEYCODE_BACK)
{
                Intent myIntent = new Intent(CurrentClass.this, NextActivity.class);
                 startActivity(myIntent);
                return false;
}
else
    return true;       //if you not write this then your  menu and other think will be affected.

Thank you I think this little bit information will be helpful for u.

救星 2025-01-01 07:12:55

<前><代码>@Override
公共布尔onKeyDown(int keyCode,KeyEvent事件){

开关(键码)
{
案例KeyEvent.KEYCODE_BACK:
//带回之前的活动,在这里做你的逻辑
返回假;
}
返回 super.onKeyDown(keyCode, 事件);
}

您可以使用它,当不应该关闭活动时返回 false。使用 FLAG_ACTIVITY_REORDER_TO_FRONT 启动上一个 Activity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            //bring back the previous activity do your logic here           
            return false;
        }
    return super.onKeyDown(keyCode, event);
}

You could use this, return false when activity should not be closed. use FLAG_ACTIVITY_REORDER_TO_FRONT to launch the previous activity

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文