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

本文标题为:Android - 切换到活动而不重新启动它

基础教程推荐