What's packageContext in Intent#(Context packageContext, Class<?> cls)?

The document says :

packageContext A Context of the
application package implementing this

  • CoordinatorLayout not drawing behind status bar even with windowTranslucentStatus and fitsSystemWindows
  • How to set time to device programmatically
  • Android get free size of internal/external memory
  • onStart of new Activity is called before onStop of parent
  • Can't build and run an android test project created using “ant create test-project” when tested project has jars in libs directory
  • main cannot be resolved or is not a field
  • But what does it mean? which class is “this class”?

    Why new Intent(this,XXX.class) works?

    Related posts:

    Where do I create and use ScheduledThreadPoolExecutor, TimerTask, or Handler?
    android - How to create xml&#039;s id?
    White background during app launch
    Get location name from fetched coordinates
    Update Android Widget From Activity
    armeabi and armeabi-v7a folder
  • JSON on Android - serialization
  • NoClassDefFoundError: Failed resolution of: Landroid/support/v7/appcompat/R$styleable
  • ActionBar Dropdown Spinner item defaults to first item
  • Android AnimationDrawable and knowing when animation ends
  • Android Google Maps v2 - Add object to marker
  • webview cannot load html file from sd card
  • 4 Solutions collect form web for “What's packageContext in Intent#(Context packageContext, Class<?> cls)?”

    You can pass any Context object you got from any of you application’s classes. So you can either use an Activity, Service object or you can call getApplicationContext() and pass the returned Context object to the Intent constructor.

    Think of Context as a state of your Application. It is used to manipulate resources and services used by the application like database, local files, class loaders, shared preferences etc.

    When some one calls you and asks for your time what is the first thing that you say… What is this in reference to? or what is the context of this conversation? Depending on the context you can decide whether it is worth proceeding with the call or not. If you do proceed then conversation should be relatively easy as you already know the context.

    Same goes with Intents. What is an Intent? Something that has an intention – One of it is to Start an Activity.

    Intent intent = new Intent(getApplicationContext(),ActivityTwo.class);

    If this makes your concept a little more clear than lets proceed to your question and the actual API –

    Intent#(Context packageContext, Class<?> cls)

    As you have notices first argument should be of type Context which we know is a abstract class. Typically you can pass any concrete class that extends Context class – Activity class being one of them (in which case you simply use this in the argument).

    enter image description here

    I think it is meant to be the context related to the application package implementing the current class.

    For example, if you are in a dog class and you have the following package structure com.mycompany.dogfinder.dog, then the package context refers to the context associated with the package that implements the dog class. In this instance, the context associated with dog class would be the context related to the com.mycompany.dogfinder application package.

    This took me a little while to figure out what was being said, but I think that is what it means.

    For starting activity from activity:

        public SomeActivity extends Activity{
        private startAnother(){
            Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);
            startActivity(intent);// works fine
            getApplicationContext().startActivity(intent)//works too, 
           //but flag Intent.FLAG_ACTIVITY_NEW_TASK needed and new you will get      
           //backstack offcourse

    Intent using context for creating ComponentName only:

        public Intent(Context packageContext, Class<?> cls) {
            mComponent = new ComponentName(packageContext, cls);
    // Identifier for a specific application component
        public ComponentName(Context pkg, Class<?> cls) {
            mPackage = pkg.getPackageName();
            mClass = cls.getName();
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.