好吧标题有点怪异。我的应用程序在SplashScreenActivity
启动,显示3秒钟,然后启动SignInActivity
以允许用户登录。点击nextButton
上的SignInActivity
即会启动MainActivity
。
问题是,在我启动后点击nextButton
它会重新启动SignInActivity
并再次点击nextButton
会将我带到MainActivity
这是完全奇怪的。如果我使用任何其他方法启动SignInActivity
并点击nextButton
,则会直接转到MainActivity
,而不会出现任何问题。我已经通过我的代码顶部和底部,并且不知道可能导致此问题的原因。
这是相关代码
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isLoggedIn) {
sharedPreferences.edit().putBoolean("isLoggedIn", true).apply();
Log.d("TAG", "Logged in Shared Preferences Set");
startActivity(new Intent(SignInActivity.this, MainActivity.class));
finish();
} else {
sharedPreferences.edit().putBoolean("isLoggedIn", false).apply();
Log.d("TAG", "Not Logged in Shared Preferences Set");
startActivity(new Intent(SignInActivity.this, MainActivity.class));
Toast.makeText(SignInActivity.this, "Sign In was Skipped", Toast.LENGTH_SHORT).show();
finish();
}
}
});
有趣的是,我评论了startActivity()
方法,看它是否重新启动了活动,但事实并非如此。
这里有什么问题?
更新:生命周期日志 - >不知道为什么在第一种情况下SignInActivity
没有被销毁,以及是什么导致重建活动。
(未通过Facebook或Google +登录)
10-27 00:46:20.549 22392-22392/com.dhyanfoundation.vedicchants D/Log: SignInActivity Created
10-27 00:46:21.464 22392-22392/com.dhyanfoundation.vedicchants D/Log: handleSignInResult:false
10-27 00:46:29.593 22392-22392/com.dhyanfoundation.vedicchants D/Log: Not Logged in Shared Preferences Set
10-27 00:46:29.692 22392-22392/com.dhyanfoundation.vedicchants D/Log: MainActivity Created
10-27 00:46:30.049 22392-22392/com.dhyanfoundation.vedicchants D/Log: SignInActivity Created
10-27 00:46:30.211 22392-22392/com.dhyanfoundation.vedicchants D/Log: handleSignInResult:false
10-27 00:46:30.473 22392-22392/com.dhyanfoundation.vedicchants D/Log: SignInActivity Destroyed
10-27 00:46:48.274 22392-22392/com.dhyanfoundation.vedicchants D/Log: Not Logged in Shared Preferences Set
10-27 00:46:48.674 22392-22392/com.dhyanfoundation.vedicchants D/Log: SignInActivity Destroyed
(通过facebook缓存登录登录)
10-27 00:51:17.411 28298-28298/com.dhyanfoundation.vedicchants D/Log: SignInActivity Created
10-27 00:51:20.378 28298-28298/com.dhyanfoundation.vedicchants D/Log: Logged in Shared Preferences Set
10-27 00:51:20.438 28298-28298/com.dhyanfoundation.vedicchants D/Log: MainActivity Created
10-27 00:51:20.701 28298-28298/com.dhyanfoundation.vedicchants D/Log: SignInActivity Created
10-27 00:51:21.100 28298-28298/com.dhyanfoundation.vedicchants D/Log: SignInActivity Destroyed
10-27 00:51:26.960 28298-28298/com.dhyanfoundation.vedicchants D/Log: Logged in Shared Preferences Set
10-27 00:51:27.326 28298-28298/com.dhyanfoundation.vedicchants D/Log: SignInActivity Destroyed
(通过Google+缓存登录登录)
10-27 00:53:29.039 30454-30454/com.dhyanfoundation.vedicchants D/Log: SignInActivity Created
10-27 00:53:33.133 30454-30454/com.dhyanfoundation.vedicchants D/Log: handleSignInResult:true
10-27 00:53:44.096 30454-30454/com.dhyanfoundation.vedicchants D/Log: Logged in Shared Preferences Set
10-27 00:53:44.204 30454-30454/com.dhyanfoundation.vedicchants D/Log: MainActivity Created
10-27 00:53:44.565 30454-30454/com.dhyanfoundation.vedicchants D/Log: SignInActivity Created
10-27 00:53:44.954 30454-30454/com.dhyanfoundation.vedicchants D/Log: SignInActivity Destroyed
10-27 00:53:45.358 30454-30454/com.dhyanfoundation.vedicchants D/Log: handleSignInResult:true
10-27 00:53:52.674 30454-30454/com.dhyanfoundation.vedicchants D/Log: Logged in Shared Preferences Set
10-27 00:53:53.120 30454-30454/com.dhyanfoundation.vedicchants D/Log: SignInActivity Destroyed
我创建了一个新的空白活动,并在MainActivity
点击方法中将BlankActivity
替换为nextButton
,一切都很好。这是生命周期日志:
10-27 00:56:19.538 1502-1502/com.dhyanfoundation.vedicchants D/Log: SignInActivity Created
10-27 00:56:22.116 1502-1502/com.dhyanfoundation.vedicchants D/Log: handleSignInResult:false
10-27 00:56:24.747 1502-1502/com.dhyanfoundation.vedicchants D/Log: Not Logged in Shared Preferences Set
10-27 00:56:24.849 1502-1502/com.dhyanfoundation.vedicchants D/Log: BlankActivity Created
10-27 00:56:25.244 1502-1502/com.dhyanfoundation.vedicchants D/Log: SignInActivity Destroyed
答案 0 :(得分:0)
首先,我认为您应该重新审视一下您的设计。当用户尝试通过应用程序登录时,检查其登录状态的逻辑应该在 SplashScreenActivity 本身的意向重定向中完成。
假设您已正确跟踪登录状态,您的活动流程应如下所示:
1) SplashScreenActivity - > MainActivity (如果用户已登录)
2) SplashScreenActivity - > SignInActivity - > MainActivity (如果用户未登录)
如果用户登录状态为false,则应将其重定向到 SignInActivity ,并在nextButton的onclick事件中,应验证其凭据,然后更新登录状态,然后他应该是重定向到 MainActivity 。
在 MainActivity 中,您应该允许用户注销,登录状态应更新为false。
我知道这并没有直接回答你的问题,但很多时候可以通过重新思考设计并使其在该级别中保持稳健来消除这样的错误。