Android重回旧活动 [英] Android going back to old activity

查看:84
本文介绍了Android重回旧活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Tab A,B和C.它们都包含一个称为Aa,Bb和Cc的ListView.当我单击这些ListView中的一个项目时,我想显示一个新活动,该活动将显示该被单击项目的信息.我已启用ActionBar中的按钮,该按钮将我带到父级Activity.但是,总是当我单击该按钮时,它确实将我带到父活动,但是选择了一个完全不同的选项卡,并且它再次加载了所有内容(列表视图的数据).基本上与启动应用程序时相同.

I have a Tab A, B and C. All ofthem contain a ListView called Aa, Bb and Cc. When I click on an item in one of these ListViews I want to show a new activity which will display the information of that clicked item. I have enabled the button in the ActionBar which takes me to the parent Activity. However always when I click that button it does take me to the parent activity but a whole different tab is selected and it loads everything again (data for the listviews). Basically it's the same as when I would start the application.

我想要回到我父母活动的最后一个状态".我覆盖了onNavigateUp()方法.

What I want is to go back to the last "state" of my parent activity. I overrode the onNavigateUp() method.

@Override
public boolean onNavigateUp() {
    onBackPressed();
    return false;
}

这有效.它返回到父活动并以我离开它的状态显示它,也就是说,我所在的选项卡仍处于选中状态,而ListView显示我上次看到的项目.

This works. It goes back to the parent activity and shows it in the state I left it, that is, the tab I was in is still selected and the ListView shows me the items I last saw.

但是,现在我想在单击显示器Activity中的按钮时显示通知.当我单击该通知时,它应该带我到显示Activity.

However, now I want to show a notification when I clicked a button in my display Activity. And when I click that notification it should take me to the display Activity.

到目前为止,效果很好,但是当我单击该按钮以显示通知并单击该通知时,它又带我进入显示活动.但是问题是,它已经打开,当我单击navigateUpButton时,它将带我到之前显示的最后一个活动,因此它再次是显示活动.我将不得不再次单击它以显示父活动.

So far so good but when I click that button so that the Notification shows up and I click on that notification, it takes me again to the display Activity. But the problem is, it is already open and when I click on the navigateUpButton it takes me to the last activity I was showing before so it's the display Activity again. I would have to click it again to show the parent activity.

我希望我的问题很清楚.当我单击该navigateUpButton时如何显示其上一个活动的父活动?

I hope my problem is clear. How can I show the parent activity in its last showed state when I click that navigateUpButton?

在类似的问题中,我看到了一种将其添加到清单文件中的活动中的方法:

I saw in a similar question an approach to add this to my activity in the manifest file :

android:alwaysRetainTaskState="True"
android:launchMode="singleInstance"

成功了,我现在只能使用一个实例.当我单击它并且仍在我的显示活动中时,它将带我回到父活动. 但是,当我进入主屏幕并单击该通知时,将显示活动",然后使用navigateUpButton回到我的主屏幕,而不是返回给我父母Activity.

It worked, I can now only use one instance. When I click it and I am still in my display Activity it takes me back to my parent Activity. However, when I am in my home screen and click that notification, the display Activity shows up and then with the navigateUpButton I get back to my home screen and not back to my parent Activity.

推荐答案

将此保留在清单文件中:

Keep this in your manifest file:

<activity
    android:label="@string/app_name" android:name="ChildActivity"
    android:launchMode="singleTop" >

还有

在您的onNavigateUp

@Override
public boolean onNavigateUp() {
    Intent intentMain = new Intent(ChildAct.this, MainActivity.class);
    startActivity(intentMain );
    return false;
}

现在,只有在没有任何实例的情况下,才会创建"singleTop"活动的新实例来处理新的意图.但是,如果目标任务在其堆栈的顶部已经具有该活动的现有实例,则该实例将收到新的意图(在onNewIntent()调用中);否则,该任务将接收新意图.没有创建新实例

Now a new instance of a "singleTop" activity will only be created to handle a new intent if there is no any instance already. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created

在此处引用

希望这会有所帮助.

这篇关于Android重回旧活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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