Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 837
slhck Avatar asked Nov 28 '10 18:11

slhck


People also ask

How can we call method in activity from non activity class?

onCreate(savedInstanceState); setContentView(R. layout. main2); DataClass dc = new DataClass(); dc. show(); } public void call(ArrayList<String> arr) { // Some code... } }


1 Answers

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

Intent myIntent = new Intent(mContext, theNewActivity.class); mContext.startActivity(myIntent); 
like image 157
Exile Avatar answered Sep 20 '22 21:09

Exile