75

I have a map view activity that shows a few OverlayItems. In the onTap method of an overlay, I want to trigger a new activity that shows this overlay photo as a fullscreen, for example.

When I do this inside my overlay class:

Intent intent = new Intent();
intent.setClass(getApplicationContext, FullscreenView.class);
startActivity(intent);

.. it can't find an application context, as I am not in the scope of an activity.

When I add a method to my main activity, let's say startFullscreen:

public static void startFullscreen() {
    if (sCurrentPhoto != null) {
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), FullscreenView.class);
        startActivity(intent);          
    }
}

I can not call getApplicationContext() and startActivity(), because I am in a static context. I need the static method however to call it in the Overlay class like MainView.startFullscreen().

Put simply: How can I start an Activity from a non-Activity class?

slhck
  • 36,575
  • 28
  • 148
  • 201

4 Answers4

97

Once you have obtained the context in your onTap() you can also do:

Intent myIntent = new Intent(mContext, theNewActivity.class);
mContext.startActivity(myIntent);
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Exile
  • 9,163
  • 4
  • 23
  • 22
42

Your onTap override receives the MapView from which you can obtain the Context:

@Override
public boolean onTap(GeoPoint p, MapView mapView)
{
    // ...

    Intent intent = new Intent();
    intent.setClass(mapView.getContext(), FullscreenView.class);
    startActivity(intent);

    // ...
}
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • 2
    When I try to start an activity from a non-activity class (in the same process) I get an exception: "android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag." (I'm doing this in the context of an intent handler.) – Tom Sep 08 '12 at 20:11
  • before the startActivity, add `intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);` – Leonardo Goes Aug 16 '20 at 22:13
14

I don't know if this is good practice or not, but casting a Context object to an Activity object compiles fine.

Try this: ((Activity) mContext).startActivity(...)

Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
  • 3
    Unbelievably, this works. And it's the only solution here that does work for startActivityForResult(). – SMBiggs Mar 12 '12 at 04:08
  • 10
    This is only going to work if the context actually *is* an activity. E.g. I don't think you can cast the application context to an activity. – Timmmm Nov 14 '12 at 10:14
  • Timmmm is right, and btw both ApplicationContext and Activity's BaseContext extends Android.Content.Context which is an abstract class – Has AlTaiar Jun 12 '13 at 09:26
9

You can define a context for your application say ExampleContext which will hold the context of your application and then use it to instantiate an activity like this:

var intent = new Intent(Application.ApplicationContext, typeof(Activity2));
intent.AddFlags(ActivityFlags.NewTask);
Application.ApplicationContext.StartActivity(intent);

Please bear in mind that this code is written in C# as I use MonoDroid, but I believe it is very similar to Java. For how to create an ApplicationContext look at this thread

This is how I made my Application Class

    [Application]
    public class Application : Android.App.Application, IApplication
    {
        public Application(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
        {

        }
        public object MyObject { get; set; }
    }
Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37