我的Android应用程序以Facebook登录页面开始,如果用户被记录,则跳转到主页。
问题是,如果用户被记录为jet,则主页将显示一秒钟。
这是我的入门代码:
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private AccessTokenTracker mAccessTokenTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
final Context context=this;
if (AccessToken.getCurrentAccessToken() != null) {
mAccessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
mAccessTokenTracker.stopTracking();
if(currentAccessToken == null) {
Log.i("LOGIN","ALL PERMISSION DELETED, SKIP TO THE LOGIN");
}
else {
//CHECK PERMISSION ONE BY ONE
String current_permission =currentAccessToken.getPermissions().toString();
if (current_permission.contains("user_photos") && current_permission.contains("user_post") && current_permission.contains("public_profile")) {
Toast.makeText(getApplicationContext(), "YOU ARE LOGGED IN", Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, HomeActivity.class);
context.startActivity(intent); //USER IS LOGGED IN AND HAVE ALL PERMISSION SO GO TO THE HOME PAGE
}
}
}
};
AccessToken.refreshCurrentAccessTokenAsync();
}
else {
Log.i("LOGIN","NEED TO LOG IN");
}
setContentView(R.layout.activity_main);
......
//GO TO THE LOGIN PAGE
我如何避免这种情况以及什么是最佳实践?
答案 0 :(得分:0)
如果您的用户已登录,请确保在调用startActivity()后不调用setContentView()。这样,除非用户未登录,否则您不会对布局进行充气。
您可以通过多种方式执行此操作,包括在调用startActivity()之后从方法返回。