开发者

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

开发者 https://www.devze.com 2023-02-22 23:58 出处:网络
The document says : packageContextA Context of the application package implementing this class. But what does it 开发者_Go百科mean? which class is \"this class\"?

The document says :

packageContext A Context of the application package implementing this class.

But what does it 开发者_Go百科mean? which class is "this class"?

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


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).

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


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();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消