Android - 切换到活动而不重新启动它

Android - switch to activity without restarting it(Android - 切换到活动而不重新启动它)
本文介绍了Android - 切换到活动而不重新启动它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 android 编写一个聊天程序.

I am programming a chat program for android.

我将联系人列表作为一项活动,将聊天窗口作为第二项活动.我使用 startActivity 切换到聊天活动,但聊天活动每次都会重新加载.因此屏幕被清除.

I have the contact list as one activity and the chat windows as a second activity. I use startActivity to switch to the chat activity, but the chat activity gets reloaded every time. Therefore the screen gets cleared.

有没有办法切换到正在运行的活动而无需重新启动它?

Is there a way to switch to a running activity without having to restart it?

private Intent myIntent = null;

    if (myIntent == null)
        myIntent = new Intent(HanasuAndroidActivity.activity, ChatWindow.class);

    this.startActivity(myIntent);

推荐答案

FLAG_ACTIVITY_REORDER_TO_FRONT 添加到您的 Intent.这会将现有的活动实例(如果存在)带到前台,如果不存在则创建一个新实例.

Add FLAG_ACTIVITY_REORDER_TO_FRONT to your Intent. That will bring the existing activity instance to the foreground if it exists or create a new one if it does not exist.

这篇关于Android - 切换到活动而不重新启动它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)