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

The document says :

packageContext A Context of the
application package implementing this
class.

  • How to let the ImageView and the TextView the same height
  • how to get a list item position by clicking the button inside it?
  • java.lang.RuntimeException: Parcelable encountered IOException writing serializable object in Android passing ArrayList object
  • Splitting text across multiple TextViews/Fragments using ViewPager
  • Is it worth purchasing Google Android Dev phone?
  • Can't create “Share” menu in popup menu on Android
  • But what does it mean? which class is “this class”?

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

    Related posts:

    Android Fragment onAttach() deprecated
    Is there any specific reason to use Amazon SNS instead of direct Baidu service
    Remove firebase analytics from android app completely
    Different font size of strings in the same TextView
    Difference between onKey(), OnKeyDown() and dispatchKeyEvent() methods provided by Android?
    Is there a way to check if an app signature is debug or published?
  • addView does not work
  • Bonjour implementation on Android
  • Facebook Login-Logout Issue Invalid Key Hash Error, While Trying to Login Again
  • Android make a dialog appear in fullscreen
  • Calculating distance between two geographic locations
  • Check if Android WebView is consuming touch events
  • 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);
    startActivity(intent);
    

    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.