【发布时间】:2012-05-07 23:57:26
【问题描述】:
我是 android 新手,我正在尝试了解 getApplication()、getApplicationContext()、getBaseContext()、getContext() 和 someClass.this 之间的区别,尤其是在以下情况下何时使用这些方法代码行:
当我启动 toast 时,它们之间有什么区别?在什么情况下使用它们?
Toast.makeText(LoginActivity.this, "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplication(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
与意图相同:
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
Intent intent = new Intent(MenuPagina., LoginActivity.class);
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
Intent intent = new Intent(getApplication(), LoginActivity.class);
【问题讨论】:
-
他们只处理上下文和baseContext。但我想了解它们在 2 个上下文中的所有用法。谢谢
标签: android android-intent this toast android-context