请登录的每一次活动 [英] Check login on every activity

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

问题描述

我开发和应用,用户必须在使用之前登录。我定制了自己的标题栏,并把一个注销按钮出现,使用户注销时,他们想要的,并与其他帐户登录。那么应用程序应再次显示登录活动。

I'm developing and app where the user must login before using it. I customized my titlebar and put a logout button there, to enable users to logout whenever they want, and login with another account. The app should then display the login activity again.

这工作得很好,但我的问题是后退按钮。当用户注销时,登录活动露面,但如果他presses回,应用程序返回到previous页,使用户再次使用该活动,即使没有登录。

That works well, but my problem is with the back button. When the user logs out, the login activity show up, but if he presses back, the app returns to the previous page, enabling the user to use that activity again, even without login.

我有我的的onCreate()的每一次活动设立这样的:

I have my onCreate() of every activity set up this way:

public void onCreate(Bundle savedInstanceState) {  
    // Activity code  
    checkLogin();  
}

checkLogin()

if (GlobalContext.getCurrentUser() == null) {
    Intent i = new Intent(this, LoginActivity.class);
    startActivityForResult(i, GlobalContext.REQUEST_LOGIN);
}

不过这不工作像它应该,我的应用程序崩溃有时当我preSS回来,有时它返回到previous活动就像我说的,有时候它的实际工作。

However that's not working like it should, my app crashes sometimes when I press back, sometimes it return to the previous activity like I said, and sometimes it does actually work.

我怎样才能让 checkLogin()被调用时每次(重新)启动的活动?
或者有其他更好的方式做我想要什么?

How can I make the checkLogin() get called everytime the activity is (re)started?
Or there is another, better way to do what I want?

编辑:

根据@加布里埃尔的答案,并从的SDK引用的,我移动 checkLogin() onResume()方法,而不是的onCreate ,解决我的问题。

Based on @Gabriel's answer and the following diagram from the SDK references, I moved the checkLogin() to the onResume() method instead of the onCreate, solving my problem.

推荐答案

将调用 checkLogin()的onCreate() ONSTART()

这篇关于请登录的每一次活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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