Android的 - 切换到活动,而不需要重启 [英] Android - switch to activity without restarting it

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

问题描述

我为Android编写一个聊天程序。

我的联系人列表中的一个活动,并在聊天窗口作为第二个活动。 我用startActivity切换到聊天活动,但聊天活动被重新加载每一次。因此在屏幕被清除。

有没有一种方法,而无需重新启动切换到正在运行的活动呢?

 私人意图myIntent = NULL;
 

...

 如果(myIntent == NULL)
        myIntent =新的意图(HanasuAndroidActivity.activity,ChatWindow.class);

    this.startActivity(myIntent);
 

解决方案

FLAG_ACTIVITY_REORDER_TO_FRONT 添加到您的意图。这将带来现有活动实例的前景如果它存在或创建一个新的,如果它不存在。

I am programming a chat program for android.

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);

解决方案

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

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