Close the current activity when you only have a reference to Context
yes, with a cast: ((Activity) ctx).finish();
yes, with a cast: ((Activity) ctx).finish();
Ok, I will give a small example on how to do what you ask public class ClassB extends Activity { ClassA A1 = new ClassA(this); // for activity context ClassA A2 = new ClassA(getApplicationContext()); // for application context. }
getApplicationContext() Application context is associated with the Applicaition and will always be the same throughout the life cycle. getBasecontext() should not be used, just use Context instead of it which is associated with the activity and could possible be destroyed when the activity is destroyed. getApplication() is available to Activity and Services only. Although in … Read more
As far as I understand: Context is the Base Object. So every Activity same as Application derives from Context. This means that every Activity and every Application IS a Context; From developer.android.com Activity java.lang.Object ↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.view.ContextThemeWrapper ↳ android.app.Activity And Application java.lang.Object ↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.app.Application An Application context lasts, … Read more
You will have to pass a context object to it. Either this if you have a reference to the class in an activty, or getApplicationContext() public class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) { RegularClass regularClass = new RegularClass(this); } } Then you can use it in the constructor (or set it to … Read more
If your class is non-activity class, and creating an instance of it from the activiy, you can pass an instance of context via constructor of the later as follows: class YourNonActivityClass{ // variable to hold context private Context context; //save the context recievied via constructor in a local variable public YourNonActivityClass(Context context){ this.context=context; } } … Read more
Toast and Intent, both requires reference to context. And getApplication, getApplicationContext, LoginActivity.this and getBaseContext, they all offer reference to the context. Now the thing confuses is the declaration of different contexts and their specific-usage. To make things simple, you should count two types of context available in the Android framework. Application Context Activity Context Application … Read more
You can use an Application context which is provided by the AndroidViewModel, you should extend AndroidViewModel which is simply a ViewModel that includes an Application reference.
Drawable d = getResources().getDrawable(android.R.drawable.ic_dialog_email); ImageView image = (ImageView)findViewById(R.id.image); image.setImageDrawable(d);
You have a few options here: Pass Context as an argument to FeedAdapter and keep it as class field Use dependency injection to inject Context when you need it. I strongly suggest reading about it. There is a great tool for that — Dagger by Square Get it from any View object. In your case … Read more