Elaborating Alex Volovoy's answer a little more -
in case u are getting this problem with fragments, getActivity() works fine to get the context
In Other Cases:
If you don't want to use-
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//not recommend
then make a function like this in your OutsideClass -
public void gettingContext(Context context){
real_context = context;//where real_context is a global variable of type Context
}
Now,in your main activity when ever you make a new OutsideClass call the above method immediately after you define the OutsideClass giving the activity's context as argument.
Also in your main activity make a function-
public void startNewActivity(final String activity_to_start) {
if(activity_to_start.equals("ACTIVITY_KEY"));
//ACTIVITY_KEY-is a custom key,just to
//differentiate different activities
Intent i = new Intent(MainActivity.this, ActivityToStartName.class);
activity_context.startActivity(i);
}//you can make a if-else ladder or use switch-case
now come back to your OutsideClass,and to start new activity do something like this-
@Override
public void onClick(View v) {
........
case R.id.any_button:
MainActivity mainAct = (MainActivity) real_context;
mainAct.startNewActivity("ACTIVITY_KEY");
break;
}
........
}
This way you will be able to start different activities called from different OutsideClass without messing up with flags.
Note-Try not to cache context object via constructor for fragment(with adapter,its fine).A fragment should have a empty constructor otherwise application crashes in some scenarios.
remember to call
OutsideClass.gettingContext(Context context);
in the onResume() function as well.