Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Toast</strong> and <strong>Intent</strong>, both requires reference to <em>context</em>. And <strong>getApplication</strong>, <strong>getApplicationContext</strong>, <strong>LoginActivity.this</strong> and <strong>getBaseContext</strong>, they all offer reference to the context.</p> <p>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.</p> <ol> <li>Application Context</li> <li>Activity Context</li> </ol> <p><strong>Application</strong> context is attached to the application's life-cycle and will always be same throughout the life of application. So if you are using <em>Toast</em>, you can use application context or even activity context (both) because a toast can be raised from anywhere with in your application and is not attached to a window.</p> <p><strong>Activity</strong> context is attached to the Activity's life-cycle and can be destroyed if the activity's <code>onDestroy()</code> is raised. If you want to launch a new activity, you must need to use activity's context in its <em>Intent</em> so that the new launching activity is connected to the current activity (in terms of activity stack). However, you may use application's context too to launch a new activity but then you need to set flag <code>Intent.FLAG_ACTIVITY_NEW_TASK</code> in intent to treat it as a new task.</p> <p><em>Now referring to your cases:</em></p> <p><code>LoginActivity.this</code> though its referring to your own class which extends Activity class but the base class (Activity) also extends Context class, so it can be used to offer activity context.</p> <p><code>getApplication()</code> though its referring to Application object but the Application class extends Context class, so it can be used to offer application context.</p> <p><code>getApplicationContext()</code> offers application context.</p> <p><code>getBaseContext()</code> offers activity context.</p> <blockquote> <p><b>Tips: Whenever you need to manipulate <code>Views</code> then go for <em>Activity-Context</em>, else <em>Application-Context</em> would be enough.</b></p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload