活动中的“后退"按钮,而无需重新创建先前的活动(Android) [英] Back button in an Activity without recreate previous Activity (Android)

查看:53
本文介绍了活动中的“后退"按钮,而无需重新创建先前的活动(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主活动,即菜单(A).活动A有6个片段.其中之一是图片库(图1).触摸图片时,转到图片详细信息活动(B)(图2).我将标签"parentName"添加到AndroidManifest.xml中的活动B.因此,活动B的父级是活动A.问题是:当您处于活动B中并按下向上"按钮(图2)时,将重新创建活动A,并显示活动A的第一个片段(而不是活动A).画廊片段).当您返回上一个活动时,我希望Instragram应用程序具有相同的行为.似乎Instagram应用程序没有重新创建之前的活动.预期的行为也类似于后退按钮的行为(图3).我该如何实现这种行为?谢谢

I have a master activity that is a menu (A). The activity A have 6 fragments. One of them is a gallery of pictures (Figure 1). When you touch a picture you go to the picture details activity (B) (Figure 2). I add the tag "parentName" to the activity B in the AndroidManifest.xml. So the parent of the activity B is the activity A. The problem is: when you are in activity B and you press the Up Button (Figure 2) the activity A is recreated and it shows the first fragment of the activity A (not the gallery fragment). I want the same behavior of the Instragram app when you back to a previous activity. It seems like the Instagram app don't recreate the previous activity. The expected behavior is also similar to the behavior of the back button (Figure 3). How can I achieve this behavior? Thanks

图1.图片详细信息活动

图2.图片详细信息活动

图3.后退按钮

推荐答案

我已经轻松解决了我的问题.我只需要在活动A中添加标签 android:launchMode ="singleTop" .我还可以在活动A中添加下一个代码,但这可能不是必需的.我希望您觉得它有用.

I've solved my problem easily. I just had to add the tag android:launchMode="singleTop" to the activity A. I also add the next code in the activity A but probably isn't necessary. I hope you find it useful.

@Override
protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedInstanceState);
}

这篇关于活动中的“后退"按钮,而无需重新创建先前的活动(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆