Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is different between MainActivity.this vs getApplicationContext()

Tags:

android

I am trying ProgressDialog.But I am confuse.

1.pd=ProgressDialog.show(MainActivity.this, "", "Fething data");

when I do use (MainActivity.this) then it is ok. But

2.pd=ProgressDialog.show(getApplicationContext(), "", "Fething data");

When I do use (getApplicationContext()) it is ERROR.

What is problem for this progressDialog?

What is different between (MainActivity.this) vs (getApplicationContext())

and when I use it perfect time?

For getApplicationContext() Error is:

04-09 15:05:37.453: E/AndroidRuntime(9980): FATAL EXCEPTION: main 04-09 15:05:37.453: E/AndroidRuntime(9980): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:571) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.app.Dialog.show(Dialog.java:281) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.app.ProgressDialog.show(ProgressDialog.java:116) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.app.ProgressDialog.show(ProgressDialog.java:99) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.app.ProgressDialog.show(ProgressDialog.java:94) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at com.example.shikkok_services.MainActivity$2.onClick(MainActivity.java:27) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.view.View.performClick(View.java:4204) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.view.View$PerformClick.run(View.java:17355) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.os.Handler.handleCallback(Handler.java:725) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.os.Handler.dispatchMessage(Handler.java:92) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.os.Looper.loop(Looper.java:137) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.app.ActivityThread.main(ActivityThread.java:5041) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at java.lang.reflect.Method.invokeNative(Native Method) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at java.lang.reflect.Method.invoke(Method.java:511) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-09 15:05:37.453: E/AndroidRuntime(9980):     at dalvik.system.NativeStart.main(Native Method) 
like image 665
AndyError Avatar asked Apr 09 '14 15:04

AndyError


People also ask

What is the difference between this context and getApplicationContext which one to use when?

This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component. Actually in general it is better to use getApplicationContext() since it will less likey lead to memory leaks.

What is the difference between getContext () getApplicationContext () getBaseContext () and this?

getApplicationContext() - Returns the context for all activities running in application. getBaseContext() - If you want to access Context from another context within application you can access. getContext() - Returns the context view only current running activity.

What is use of getApplicationContext?

When we call a method or a constructor, we often have to pass a Context and often we use “this” to pass the activity Context or “getApplicationContext” to pass the application Context. This method is generally used for the application level and can be used to refer to all the activities.

What is the difference between getActivity and getContext?

getContext() - Returns the context view only current running activity. getActivity()- Return the Activity this fragment is currently associated with. getActivity() can be used in a Fragment for getting the parent Activity of the Fragment .


2 Answers

Which context to use?

There are two types of Context:

Application context is associated with the application and will always be the same throughout the life of application; it does not change. So if you are using Toast, you can use application context or even activity context (both) because Toast can be displayed from anywhere within your application and is not attached to a specific window. But there are many exceptions. One such exception is when you need to use or pass the activity context.

Activity context is associated with the activity and can be destroyed if the activity is destroyed; there may be multiple activities (more than likely) with a single application. Sometimes you absolutely need the activity context handle. For example, should you launch a new Activity, you need to use activity context in its Intent so that the newly-launched activity is connected to the current activity in terms of activity stack. However, you may also use application's context to launch a new activity, but then you need to set flag Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.

Let's consider some cases:

MainActivity.this refers to the MainActivity context which extends Activity class but the base class (Activity) also extends Context class, so it can be used to offer activity context.

getBaseContext() offers activity context.

getApplication() offers application context.

getApplicationContext() also offers application context.

For more information please check this link.

like image 180
Zohra Khan Avatar answered Sep 22 '22 14:09

Zohra Khan


  • MainActivity.this only works if you are in an inner class of MainActivity.

  • If you are in MainActivity itself, just use this.

  • If you are in another class entirely, you need to pass it an instance of a context from the Activity you are in.

Hope this helps..

like image 32
Varun Avatar answered Sep 23 '22 14:09

Varun