Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Static" startActivity(Intent) method?

Tags:

android

I have a Button. Its View.OnClickHandler-implementing class is instantiated about 3 constructors deep from the nearest reference to an android.app.Activity object. When clicked, I want it to open the Location settings panel so the user can enable GPS and/or network-based location by launching the Settings.ACTION_LOCATION_SOURCE_SETTINGS intent.

Short of promiscuously passing that parent Activity object from constructor to constructor to constructor so my onClick() method can see it, is there any way to just reach up into the metaphorical static ether and scream, "Hey, Android... launch Settings.ACTION_LOCATION_SOURCE_SETTINGS" without having to have an actual live Activity object handy to use for its startActivity method?

like image 440
Bitbang3r Avatar asked Dec 08 '25 10:12

Bitbang3r


1 Answers

This worked for me:

public class StartGame extends Activity {

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

    public static void nextPass(Activity context) {
        Intent intent = new Intent(context,your.class);
        context.startActivity(intent);
    }

}
like image 57
ademar111190 Avatar answered Dec 09 '25 23:12

ademar111190



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!