Alex Rivera | Logout

How to reference the current or main activity from another class

Asked 2012-06-27T13:40:54.560
76

I often find myself needing to access methods that require referencing some activity. For example, to use getWindowManager, I need to access some Activity. But often my code for using these methods is in some other class that has no reference to an activity. Up until now, I've either stored a reference to the main activity or passed the context of some activity to the class. Is there some better way to do this?

Edit
Report

1 Answer

0

Handle the Intent in the class you want to do these methods, and send your information to it in a Bundle like so:

    Intent i = new Intent("android.intent.action.MAIN");
    i.setComponent(new ComponentName("com.my.pkg","com.my.pkg.myActivity"));
    Bundle data = new Bundle();

    i.putExtras(data);

    startActivityForResult(i);

Then use an OnActivityResultListener to grab the new data.

answered 2012-06-27T13:49:12.030

Your Answer