Hola everyone,
While working on android project i got myself into a situation where i had to start an activity from non-activity class (a class that doesn’t extend Activity), and i had no other option that time. So lets say there is a class called DummyClass having a dummyMethod() and i had to start an activity called MeActivity, so i wrote this to start an activity, a rough pseudocode:
…
dummyMethod(){
Intent dummyIntent = new Intent(DummyClass.this, MeActivity.class);
this.startActivity(dummyIntent);
}
…
}
Well as i had my doubts earlier above code is a garbage cuz there is no startActivity() method in my dummyclass. So i did something which worked for me. I declared a public static Context reference variable in MeActivity class and in its constructor i assigned its instance to variable. Here is the code that will look like:
public static Context meActivityContext;
public FavoritesActivity(){
MeActivity.meActivityContext=this;
}
}
So that was it. Now i used this context variable to start my activity from non-activity class.
…
dummyMethod(){
Intent dummyIntent = new Intent(MeActivity.meActivityContext, MeActivity.class);
MeActivity.meActivityContext.startActivity(dummyIntent);
}
…
}
well that worked for me, hope it works for you too.
Gracias.