【问题标题】:Why is New Handler is deprecated? [duplicate]为什么不推荐使用 New Handler? [复制]
【发布时间】:2021-09-12 13:20:59
【问题描述】:
public class SpalshScreen extends AppCompatActivity {


FirebaseUser currentUser;

private FirebaseAuth mAuth;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_spalsh_screen);

    mAuth = FirebaseAuth.getInstance();

    if (mAuth != null) {

        currentUser = mAuth.getCurrentUser();

    }

    new Handler().postDelayed(new Runnable() {

        @Override

        public void run() {

            FirebaseUser user = mAuth.getCurrentUser();

            if (user == null) {

                Intent intent = new Intent(SpalshScreen.this, LoginActivity.class);

                startActivity(intent);

                finish();

            } else {

                Intent mainIntent = new Intent(SpalshScreen.this, DashboardActivity.class);

                mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

                startActivity(mainIntent);

                finish();

            }

        }

    }, 1000);

}
}

【问题讨论】:

    标签: android android-handler


    【解决方案1】:

    试试下面的代码。仅弃用无参数构造函数,现在通过 Looper.getMainLooper() 方法在构造函数中指定 Looper。

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                
            }
        }, 1000);
    

    【讨论】:

      猜你喜欢
      • 2012-05-31
      • 2015-09-02
      • 2013-08-02
      • 2012-01-08
      • 1970-01-01
      • 2013-05-27
      • 2016-02-23
      • 2017-11-04
      相关资源
      最近更新 更多