Getting activity from context in android

This one has me stumped.

I need to call an activity method from within a custom layout class. The problem with this is that I don't know how to access the activity from within the layout.

ProfileView

public class ProfileView extends LinearLayout
{
    TextView profileTitleTextView;
    ImageView profileScreenImageButton;
    boolean isEmpty;
    ProfileData data;
    String name;

    public ProfileView(Context context, AttributeSet attrs, String name, final ProfileData profileData)
    {
        super(context, attrs);
        ......
        ......
    }

    //Heres where things get complicated
    public void onClick(View v)
    {
        //Need to get the parent activity and call its method.
        ProfileActivity x = (ProfileActivity) context;
        x.activityMethod();
    }
}

ProfileActivity

public class ProfileActivityActivity extends Activity
{
    //In here I am creating multiple ProfileViews and adding them to the activity dynamically.

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_activity_main);
    }

    public void addProfilesToThisView()
    {
        ProfileData tempPd = new tempPd(.....)
        Context actvitiyContext = this.getApplicationContext();
        //Profile view needs context, null, name and a profileData
        ProfileView pv = new ProfileView(actvitiyContext, null, temp, tempPd);
        profileLayout.addView(pv);
    }
}

As you can see above, I am instantiating the profileView programatically and passing in the activityContext with it. 2 questions:

  • Am i passing the correct context into the Profileview?
  • How do I get the containing activity from the context?

  • From your Activity , just pass in this as the Context for your layout:

    ProfileView pv = new ProfileView(this, null, temp, tempPd);
    

    Afterwards you will have a Context in the layout, but you will know it is actually your Activity and you can cast it so that you have what you need:

    Activity activity = (Activity) context;
    

  • No
  • You can't
  • There are two different contexts in Android. One for your application (Let's call it the BIG one) and one for each view (let's call it the activity context).

    A linearLayout is a view, so you have to call the activity context. To call it from an activity, simply call "this". So easy isn't it?

    When you use

    this.getApplicationContext();
    

    You call the BIG context, the one that describes your application and cannot manage your view.

    A big problem with Android is that a context cannot call your activity. That's a big deal to avoid this when someone begins with the Android development. You have to find a better way to code your class (or replace "Context context" by "Activity activity" and cast it to "Context" when needed).

    Regards.


    Just to update my answer. The easiest way to get your Activity context is to define a static instance in your Activity . For example

    public class DummyActivity extends Activity
    {
        public static DummyActivity instance = null;
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
    
            // Do some operations here
        }
    
        @Override
        public void onResume()
        {
            super.onResume();
            instance = this;
        }
    
        @Override
        public void onPause()
        {
            super.onPause();
            instance = null;
        }
    }
    

    And then, in your Task , Dialog , View , you could use that kind of code to get your Activity context :

    if (DummyActivity.instance != null)
    {
        // Do your operations with DummyActivity.instance
    }
    

    This is something that I have used successfully to convert Context to Activity when operating within the UI in fragments or custom views. It will unpack ContextWrapper recursively or return null if it fails.

    public Activity getActivity(Context context)
    {
        if (context == null)
        {
            return null;
        }
        else if (context instanceof ContextWrapper)
        {
            if (context instanceof Activity)
            {
                return (Activity) context;
            }
            else
            {
                return getActivity(((ContextWrapper) context).getBaseContext());
            }
        }
    
        return null;
    }
    
    链接地址: http://www.djcxy.com/p/68010.html

    上一篇: 制作自己的Windows 8应用主题

    下一篇: 在android中从上下文获取活动