PDA

View Full Version : کلاس application



smemamian
پنج شنبه 16 آبان 1392, 10:09 صبح
سلام دوستان
در مورد این کلاس مطالب زیادی خوندم و مثال های مختلفی بوده !
مثلاً اومده بودن یک کلاس نوشته بودن و extends کرده بودن از کلاس application !
این کلاس یک سری متغییر داشت ! و در کلاس دیگری یک شی از این کلاس extends شده از application رو فراخوانی می کرد و متغییر رو کاهش و یا افزایش می داد !

خب سوال اینه که با یک کلاس معمولی هم می شود چنین کاری کرد ! و نیازی به extends کردن از application نیست !!!

کسی می تونه یک مثال (منظورم کد نیست ! منظورم توضیحی است) بزنه ؟!
تشکر

farhadfery
پنج شنبه 16 آبان 1392, 13:41 عصر
The more general problem you are encountering is how to save state across several Activities and all parts of your application. A static variable (for instance, a singleton) is a common Java way of achieving this. I have found however, that a more elegant way in Android is to associate your state with the Application context.

As you know, each Activity is also a Context, which is information about its execution environment in the broadest sense. Your application also has a context, and Android guarantees that it will exist as a single instance across your application.

منبع: http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables