如何重新推出的Andr​​oid应用程序时定义启动活动? [英] How to define started activity when relaunching Android application?

查看:122
本文介绍了如何重新推出的Andr​​oid应用程序时定义启动活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的Andr​​oid和我目前工作的一个简单的XMPP客户端。用户应当能够登录,并且只要一个XMPP的消息到达时,应通知。它应该是可能的访问当前会话期间到达的所有消息的概述

I am fairly new to Android, and am currently working on a simple XMPP Client. A user should be able to log in, and should be notified whenever an XMPP message arrives. It should be possible to access an overview of all messages that arrived during the current session.

在启动应用程序,一个LoginActivity启动,提示用户填写他或她的凭据。如果提供正确的凭据,后台服务已经启动:

When launching the application, a LoginActivity is started, prompting the user to fill in his or her credentials. If the right credentials are provided, a background service is started:

Intent intent = new Intent(this, NotificationService.class);
startService(intent);

在启动时,通知服务增加了一个数据包侦听到XMPP连接,并请求将在前台运行。该用户会被提示所致,这种前景请求(正在进行)的通知。现在,我已经提供了一个名为XMPPClientActivity第二个活动,显示出在了会议,并简单的注销按钮期间收到的所有邮件。当打开从正在进行的通知中的应用,XMPPClientActivity开始因为通知是这样定义的:

On startup, the notification service adds a packet listener to the XMPP connection and requests to be running in the foreground. The users is prompted with a notification caused by this foreground request ("Ongoing"). Now I have provided a second activity called XMPPClientActivity, showing all messages that are received during the session and a simple logout button. When opening the application from within the "Ongoing" notification, the XMPPClientActivity is started because the notification is defined like this:

xmppIntent = new Intent(this, XMPPClientActivity.class);
pendingIntent = PendingIntent.getActivity(this, 0, xmppIntent, 0);
NotificationCompat.Builder xmppBuilder = new NotificationCompat.Builder(this);
xmppBuilder.setContentIntent(pendingIntent);
// Notification details
startForeground(id, xmppBuilder.build());

在从主屏幕但是打开应用程序时,LoginActivity再次打开。当然,我希望XMPPActivity要启动,但我似乎无法弄清楚如何应该这样做。我一直在寻找到一个活动绑定到一个服务,但我不能确定这是否可以帮上什么忙。什么是正确的方式做到这一点?

When opening the application from the home screen however, the LoginActivity is opened again. Of course I want the XMPPActivity to be started, but I can't seem to figure out how this should be done. I have been looking into binding an activity to a service, but I'm unsure if this can be of any help. What is the right way to do this?

推荐答案

你可以做什么,因为我知道你发出,是使用的 Shaered preferences 。创建preference像的loggedIn,并设置一个布尔变量在第一次登录。现在,您可以将其设置为时,点击注销按钮

What you can do, as I understand you issue, is use ShaeredPreferences. Create a preference like "loggedin" and set a boolean variable to true the first time they log in. Now you can set this to false when they click the "logout" Button.

活动开始,你可以检查共享preference 调用的setContentView(),如果该值然后完成() LoginActivity 键,打开你的其他活动

When the Activity is started you can check the SharedPreference before calling setContentView() and if the value is true then finish() the LoginActivity and open your other Activity.

链接到我提供的文档有一个很好的例子创建,打开和编辑共享preferences

The link to the docs I provided has a good example of creating, opening, and editing SharedPreferences

这篇关于如何重新推出的Andr​​oid应用程序时定义启动活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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