AlertDialog - Activity vs Application Context





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Can someone please explain why I have to use my Activity's context in my AlertDialog.Builder parameter VS getApplicationContext()? Specifically, why am I getting this error:



java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.


I understand the differences between the two but am wondering why specifically it mentions using Theme.AppCompat



I know that it has something to do with AppCompatActivity and themes no longer being compatible but can't quite find a solid explanation on why?



The theme of my app is set to this in the styles XML file:



    <style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>


Thank you










share|improve this question

























  • I think this has been answered: stackoverflow.com/questions/9122627/…

    – Paul
    Dec 22 '16 at 13:15






  • 2





    Possible duplicate of Why does AlertDialog.Builder(Context context) only accepts Activity as a parameter?

    – mlidal
    Dec 22 '16 at 13:16











  • This is answer on a different question is the probable answer you are looking for : stackoverflow.com/a/10347346/2730218

    – skarfa
    Dec 22 '16 at 13:18


















0















Can someone please explain why I have to use my Activity's context in my AlertDialog.Builder parameter VS getApplicationContext()? Specifically, why am I getting this error:



java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.


I understand the differences between the two but am wondering why specifically it mentions using Theme.AppCompat



I know that it has something to do with AppCompatActivity and themes no longer being compatible but can't quite find a solid explanation on why?



The theme of my app is set to this in the styles XML file:



    <style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>


Thank you










share|improve this question

























  • I think this has been answered: stackoverflow.com/questions/9122627/…

    – Paul
    Dec 22 '16 at 13:15






  • 2





    Possible duplicate of Why does AlertDialog.Builder(Context context) only accepts Activity as a parameter?

    – mlidal
    Dec 22 '16 at 13:16











  • This is answer on a different question is the probable answer you are looking for : stackoverflow.com/a/10347346/2730218

    – skarfa
    Dec 22 '16 at 13:18














0












0








0








Can someone please explain why I have to use my Activity's context in my AlertDialog.Builder parameter VS getApplicationContext()? Specifically, why am I getting this error:



java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.


I understand the differences between the two but am wondering why specifically it mentions using Theme.AppCompat



I know that it has something to do with AppCompatActivity and themes no longer being compatible but can't quite find a solid explanation on why?



The theme of my app is set to this in the styles XML file:



    <style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>


Thank you










share|improve this question
















Can someone please explain why I have to use my Activity's context in my AlertDialog.Builder parameter VS getApplicationContext()? Specifically, why am I getting this error:



java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.


I understand the differences between the two but am wondering why specifically it mentions using Theme.AppCompat



I know that it has something to do with AppCompatActivity and themes no longer being compatible but can't quite find a solid explanation on why?



The theme of my app is set to this in the styles XML file:



    <style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>


Thank you







android android-alertdialog applicationcontext






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 22 '16 at 13:38







Mark F

















asked Dec 22 '16 at 13:13









Mark FMark F

6582932




6582932













  • I think this has been answered: stackoverflow.com/questions/9122627/…

    – Paul
    Dec 22 '16 at 13:15






  • 2





    Possible duplicate of Why does AlertDialog.Builder(Context context) only accepts Activity as a parameter?

    – mlidal
    Dec 22 '16 at 13:16











  • This is answer on a different question is the probable answer you are looking for : stackoverflow.com/a/10347346/2730218

    – skarfa
    Dec 22 '16 at 13:18



















  • I think this has been answered: stackoverflow.com/questions/9122627/…

    – Paul
    Dec 22 '16 at 13:15






  • 2





    Possible duplicate of Why does AlertDialog.Builder(Context context) only accepts Activity as a parameter?

    – mlidal
    Dec 22 '16 at 13:16











  • This is answer on a different question is the probable answer you are looking for : stackoverflow.com/a/10347346/2730218

    – skarfa
    Dec 22 '16 at 13:18

















I think this has been answered: stackoverflow.com/questions/9122627/…

– Paul
Dec 22 '16 at 13:15





I think this has been answered: stackoverflow.com/questions/9122627/…

– Paul
Dec 22 '16 at 13:15




2




2





Possible duplicate of Why does AlertDialog.Builder(Context context) only accepts Activity as a parameter?

– mlidal
Dec 22 '16 at 13:16





Possible duplicate of Why does AlertDialog.Builder(Context context) only accepts Activity as a parameter?

– mlidal
Dec 22 '16 at 13:16













This is answer on a different question is the probable answer you are looking for : stackoverflow.com/a/10347346/2730218

– skarfa
Dec 22 '16 at 13:18





This is answer on a different question is the probable answer you are looking for : stackoverflow.com/a/10347346/2730218

– skarfa
Dec 22 '16 at 13:18












1 Answer
1






active

oldest

votes


















1














There are two types of Context:



Application context is associated with the application and will always be same throughout the life of application -- it does not change. So if you are using Toast, you can use application context or even activity context (both) because toast can be displayed from anywhere with in your application and is not attached to a specific window. But there are many exceptions, one exception is when you need to use or pass the activity context.



Activity context is associated with to the activity and can be destroyed if the activity is destroyed -- there may be multiple activities (more than likely) with a single application. And sometimes you absolutely need the activity context handle. For example, should you launch a new activity, you need to use activity context in its Intent 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 Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.



For more details






share|improve this answer


























  • I edited my question slightly. I'm more curious as to why I am getting this error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.....What do the contexts have to do with the Theme that I am using?

    – Mark F
    Dec 22 '16 at 13:40











  • @MarkF What you have exteneded for your activity?

    – Sridhar
    Dec 22 '16 at 13:45











  • AppCompatActivity

    – Mark F
    Dec 22 '16 at 13:48











  • @MarkF I have cross checked and it doesn't show any error. May be you did any minor mistake.Post here once you found the mistake.

    – Sridhar
    Dec 22 '16 at 14:23












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41284033%2falertdialog-activity-vs-application-context%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














There are two types of Context:



Application context is associated with the application and will always be same throughout the life of application -- it does not change. So if you are using Toast, you can use application context or even activity context (both) because toast can be displayed from anywhere with in your application and is not attached to a specific window. But there are many exceptions, one exception is when you need to use or pass the activity context.



Activity context is associated with to the activity and can be destroyed if the activity is destroyed -- there may be multiple activities (more than likely) with a single application. And sometimes you absolutely need the activity context handle. For example, should you launch a new activity, you need to use activity context in its Intent 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 Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.



For more details






share|improve this answer


























  • I edited my question slightly. I'm more curious as to why I am getting this error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.....What do the contexts have to do with the Theme that I am using?

    – Mark F
    Dec 22 '16 at 13:40











  • @MarkF What you have exteneded for your activity?

    – Sridhar
    Dec 22 '16 at 13:45











  • AppCompatActivity

    – Mark F
    Dec 22 '16 at 13:48











  • @MarkF I have cross checked and it doesn't show any error. May be you did any minor mistake.Post here once you found the mistake.

    – Sridhar
    Dec 22 '16 at 14:23
















1














There are two types of Context:



Application context is associated with the application and will always be same throughout the life of application -- it does not change. So if you are using Toast, you can use application context or even activity context (both) because toast can be displayed from anywhere with in your application and is not attached to a specific window. But there are many exceptions, one exception is when you need to use or pass the activity context.



Activity context is associated with to the activity and can be destroyed if the activity is destroyed -- there may be multiple activities (more than likely) with a single application. And sometimes you absolutely need the activity context handle. For example, should you launch a new activity, you need to use activity context in its Intent 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 Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.



For more details






share|improve this answer


























  • I edited my question slightly. I'm more curious as to why I am getting this error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.....What do the contexts have to do with the Theme that I am using?

    – Mark F
    Dec 22 '16 at 13:40











  • @MarkF What you have exteneded for your activity?

    – Sridhar
    Dec 22 '16 at 13:45











  • AppCompatActivity

    – Mark F
    Dec 22 '16 at 13:48











  • @MarkF I have cross checked and it doesn't show any error. May be you did any minor mistake.Post here once you found the mistake.

    – Sridhar
    Dec 22 '16 at 14:23














1












1








1







There are two types of Context:



Application context is associated with the application and will always be same throughout the life of application -- it does not change. So if you are using Toast, you can use application context or even activity context (both) because toast can be displayed from anywhere with in your application and is not attached to a specific window. But there are many exceptions, one exception is when you need to use or pass the activity context.



Activity context is associated with to the activity and can be destroyed if the activity is destroyed -- there may be multiple activities (more than likely) with a single application. And sometimes you absolutely need the activity context handle. For example, should you launch a new activity, you need to use activity context in its Intent 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 Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.



For more details






share|improve this answer















There are two types of Context:



Application context is associated with the application and will always be same throughout the life of application -- it does not change. So if you are using Toast, you can use application context or even activity context (both) because toast can be displayed from anywhere with in your application and is not attached to a specific window. But there are many exceptions, one exception is when you need to use or pass the activity context.



Activity context is associated with to the activity and can be destroyed if the activity is destroyed -- there may be multiple activities (more than likely) with a single application. And sometimes you absolutely need the activity context handle. For example, should you launch a new activity, you need to use activity context in its Intent 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 Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.



For more details







share|improve this answer














share|improve this answer



share|improve this answer








edited May 23 '17 at 11:53









Community

11




11










answered Dec 22 '16 at 13:16









SridharSridhar

5431821




5431821













  • I edited my question slightly. I'm more curious as to why I am getting this error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.....What do the contexts have to do with the Theme that I am using?

    – Mark F
    Dec 22 '16 at 13:40











  • @MarkF What you have exteneded for your activity?

    – Sridhar
    Dec 22 '16 at 13:45











  • AppCompatActivity

    – Mark F
    Dec 22 '16 at 13:48











  • @MarkF I have cross checked and it doesn't show any error. May be you did any minor mistake.Post here once you found the mistake.

    – Sridhar
    Dec 22 '16 at 14:23



















  • I edited my question slightly. I'm more curious as to why I am getting this error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.....What do the contexts have to do with the Theme that I am using?

    – Mark F
    Dec 22 '16 at 13:40











  • @MarkF What you have exteneded for your activity?

    – Sridhar
    Dec 22 '16 at 13:45











  • AppCompatActivity

    – Mark F
    Dec 22 '16 at 13:48











  • @MarkF I have cross checked and it doesn't show any error. May be you did any minor mistake.Post here once you found the mistake.

    – Sridhar
    Dec 22 '16 at 14:23

















I edited my question slightly. I'm more curious as to why I am getting this error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.....What do the contexts have to do with the Theme that I am using?

– Mark F
Dec 22 '16 at 13:40





I edited my question slightly. I'm more curious as to why I am getting this error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.....What do the contexts have to do with the Theme that I am using?

– Mark F
Dec 22 '16 at 13:40













@MarkF What you have exteneded for your activity?

– Sridhar
Dec 22 '16 at 13:45





@MarkF What you have exteneded for your activity?

– Sridhar
Dec 22 '16 at 13:45













AppCompatActivity

– Mark F
Dec 22 '16 at 13:48





AppCompatActivity

– Mark F
Dec 22 '16 at 13:48













@MarkF I have cross checked and it doesn't show any error. May be you did any minor mistake.Post here once you found the mistake.

– Sridhar
Dec 22 '16 at 14:23





@MarkF I have cross checked and it doesn't show any error. May be you did any minor mistake.Post here once you found the mistake.

– Sridhar
Dec 22 '16 at 14:23




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41284033%2falertdialog-activity-vs-application-context%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas