相关疑难解决方法(0)

getContext(),getApplicationContext(),getBaseContext()和"this"之间的区别

是什么区别getContext(),getApplicationContext(),getBaseContext(),和" this"?

虽然这是一个简单的问题,但我无法理解它们之间的基本区别.如果可能,请举出一些简单的例子.

android this android-context

535
推荐指数
5
解决办法
27万
查看次数

获取上下文的各种方法之间有什么区别?

在我看过的各种Android代码中:

 public class MyActivity extends Activity {
    public void method() {
       mContext = this;    // since Activity extends Context
       mContext = getApplicationContext();
       mContext = getBaseContext();
    }
 }
Run Code Online (Sandbox Code Playgroud)

但是,我找不到任何合适的解释,哪些更好,以及在什么情况下应该使用.

关于这方面的文件的指示,以及关于如果选择了错误的可能会破坏的指导,将不胜感激.

android android-context

387
推荐指数
4
解决办法
9万
查看次数

差异以及何时使用getApplication(),getApplicationContext(),getBaseContext()和someClass.this

我是新来的Android和我想明白之间的差别getApplication(),getApplicationContext()getBaseContext(),getContext()以及someClass.this特别是当使用这些方法在下面的代码行:

当我发起祝酒时,这些和我使用它们之间有什么区别?

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();
Run Code Online (Sandbox Code Playgroud)

与意图相同:

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);
Run Code Online (Sandbox Code Playgroud)

android this toast android-intent android-context

116
推荐指数
3
解决办法
5万
查看次数

标签 统计

android ×3

android-context ×3

this ×2

android-intent ×1

toast ×1