与后退按钮不必要的行为上的登录屏幕活动 [英] Unwanted behavior with back button on a login screen activity

查看:154
本文介绍了与后退按钮不必要的行为上的登录屏幕活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我建立一个应用程序,它有一个Facebook的登录按钮飞溅/登录屏幕。
用户登录后,用信息回报用户对象,我想一些数据插入到数据库中(而不是在code还,但无所谓),然后打开 MainActivity 活动。

什么情况是,第一次启动,一切工作正常:
如果用户已经登录(=有活动会话), MainActivity 活动启动。
如果用户没有登录,登录按钮显示,要求用户登录,并再次打开成功的 MainActivity 活动。

如果用户点击由 MainActivity 返回按钮,白色屏幕显示,并保持这种方式。如果后退按钮是$ P $再次pssed,应用程序关闭,当应用恢复(从最近的应用程序窗口为例),白色屏幕显示另一1-2秒,然后继续 MainActivity

我想这是因为它试图回到 SplashActivity ,但因为我完成()吧,它不能...虽然,我不是那么肯定,因为我试着用清洁backstack Intent.FLAG_ACTIVITY_CLEAR_TOP Intent.FLAG_ACTIVITY_NEW_TASK ...

什么可能导致此行为?我怎么能解决这个问题?

额外的信息:结果
  - SplashActivity 将推出与使用意图过滤器应用程序的启动在应用程序的清单:<意向滤光器> <作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/> < /意向滤光器> 结果
  - 测试在HTC感觉XE / W的Andr​​oid 4.1

下面是一些code( SplashActivity

 公共类SplashActivity延伸活动{
    私人UiLifecycleHelper uiHelper;
    私人Session.StatusCallback回调=
        新Session.StatusCallback(){
            @覆盖
            公共无效呼叫(会话的会话,SessionState会状态,异常除外){
                onSessionStateChange(会话,状态除外);
            }
      };
    私人的ArrayList<串GT;权限;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.splash);        uiHelper =新UiLifecycleHelper(这一点,回调);
        uiHelper.onCreate(savedInstanceState);        权限=新的ArrayList<串GT;();
        permissions.add(电子邮件);
        permissions.add(user_birthday);        会话的会话= Session.getActiveSession();
        如果(会话= NULL&放大器;!&安培; session.isOpened()){
            如果(!session.getPermissions()。包括(权限)){
                session.requestNewReadPermissions(新Session.NewPermissionsRequest(SplashActivity.this,权限));
            }
        }其他{
            LoginButton authButton =(LoginButton)findViewById(R.id.login_button);
            authButton.setReadPermissions(权限);
        }
    }    @覆盖
    保护无效onResume(){
        super.onResume();
        uiHelper.onResume();
    }    @覆盖
    保护无效的onPause(){
        super.onPause();
        uiHelper.onPause();
    }    私人无效onSessionStateChange(会话的会话,SessionState会状态,异常除外){
        如果(会话= NULL&放大器;!&安培; state.isOpened()){
                Request.executeMeRequestAsync(会话,新Request.GraphUserCallback(){
                    @覆盖
                    公共无效onCompleted(GraphUser用户,响应响应){
                        如果(用户!= NULL){
                                                    //没有code在这里没有
                        }
                    }
                });
                意向意图=新意图(SplashActivity.this,MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(意向);
                完();
    }
}    @覆盖
    公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,结果code,数据);
        uiHelper.onActivityResult(要求code,结果code,数据);
    }    @覆盖
    保护无效的onSaveInstanceState(捆绑outState){
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
        uiHelper.onDestroy();
    }}


解决方案

找到了解决办法!

显然,正是这种如果行:结果
如果(!session.getPermissions()。包括(权限))

什么它做的是检查是否我的权限 阵列(作为一个对象)是当前会话的权限数组中,如果它不' T(这始终是,基本上),火灾
session.requestNewReadPermissions(新Session.NewPermissionsRequest(SplashActivity.this,权限));

我所做的只是将其更改为我的权限字符串数组之一:结果
如果(!session.getPermissions()。包含(电子邮件))

和现在的作品!无白屏了。我不知道为什么会造成这样的行为虽然...也许是因为该活动不能完成()正确......不知道,但它的工作原理。

我怎么发现的:结果
简单地夹着 Log.i()中的,如果,发现它始终闪光,即使它不应该。

So I'm building an app, and it has a splash/login screen with a facebook login button. After the user logs in, and a user object with info returns, I want to insert some data into the database (not in the code yet, but doesn't matter), and then open the MainActivity activity.

What happens is that on first launch, everything works correctly: If the user is already logged in (=there is an active session), the MainActivity activity is launched. If the user is not logged in, a login button shows asking the user to log in, and again opens the MainActivity activity on success.

If a user hits the back button from MainActivity, a white screen shows, and it stays that way. If the back button is pressed again, the app closes, and when the app is resumed (from the "Recent Apps" window for example), the white screen shows for another 1-2 seconds, and then proceeds to MainActivity.

I think it happens because it tries to go back to the SplashActivity but since I finish() it, it can't... although, I'm not so sure since I tried to clean the backstack using Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK...

What could cause this behavior? How could I fix it?

Extra Info:
- SplashActivity is set to launch as the launcher of the app using intent-filter in the app's manifest:<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
- Tested on HTC Sensation XE /w Android 4.1

Here's some code (SplashActivity):

public class SplashActivity extends Activity {
    private UiLifecycleHelper uiHelper;
    private Session.StatusCallback callback =
        new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) {
                onSessionStateChange(session, state, exception);    
            }
      };
    private ArrayList<String> permissions;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);

        permissions = new ArrayList<String>();
        permissions.add("email");
        permissions.add("user_birthday");

        Session session = Session.getActiveSession();
        if(session != null && session.isOpened()) {
            if(!session.getPermissions().contains(permissions)) {
                session.requestNewReadPermissions(new Session.NewPermissionsRequest(SplashActivity.this, permissions));
            }
        } else {
            LoginButton authButton = (LoginButton) findViewById(R.id.login_button);
            authButton.setReadPermissions(permissions);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        uiHelper.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if(session != null && state.isOpened()) {
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                                                    //no code here yet
                        }
                    }
                });
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
    }
}

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

}

解决方案

Found the solution!

Apparently it was this if line:
if(!session.getPermissions().contains(permissions))

What it did is check whether my permissions ARRAY (as an object) was inside the current session's permissions array, and if it doesn't (which is always, basically), fire session.requestNewReadPermissions(new Session.NewPermissionsRequest(SplashActivity.this, permissions));.

All I did was simply change it to one of my permissions array strings:
if(!session.getPermissions().contains("email"))

And now it works! No white screen anymore. I am not sure why it caused such behavior though... maybe because the activity could not finish() properly... no idea, but it works.

How I found out:
Simply tucked Log.i() inside of the if and noticed that it always fires even though it shouldn't.

这篇关于与后退按钮不必要的行为上的登录屏幕活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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