1. Are you ready for the Galaxy S20? Here is everything we know so far!

Why activity class should (usually) be Public class?

Discussion in 'Android Development' started by Bee Torika, Feb 8, 2016.

  1. Bee Torika

    Bee Torika Newbie
    Thread Starter

    Hi ,

    Why activity class should (usually) be Public class? (Android)

    Because public class can be accessible from other package.

    And other package usually means other application, right?

    Then, why activities allow other application's accesses ?

    It looks not good for security.

    But so many projects and examples declare activities as public.

    Why not default scope?
    In default scope, other project can not access the activity and classes in project can access the activity freely.

    At least, activity that contains onCreate(Bundle bundle) will be better if it is declared as default scope, right ?

    plz tell me what is wrong with my thinking, so that I could know about android much more.
     


  2. Best Answer:
    Post #2 by Deleted User, Feb 8, 2016 (2 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. Deleted User

    Deleted User Guest

    It needs to be public, because your activity class does need to be accessed from another package. You have to realise that your app is not running in isolation on the system. In fact your app is not even a stand alone program - it has no main() method right? The app code is run by the Android app management system, which calls methods on your code - such as onCreate(). Therefore this code has to be accessible.

    However there is no danger of other apps accessing your code, because the class loader doesn't allow it. Each app effectively has its own classpath, which is used to dynamically load classes.
    Say for example my app had package name

    A.B.myapp

    and some other app was installed on the system with package name

    C.D.otherapp

    There is no way that myapp could load classes from C.D.otherapp (even if it did know the package name) because the class loader prevents it. If an attempt like that was made, myapp would get a ClassdefNotFound exception.
     
  4. Bee Torika

    Bee Torika Newbie
    Thread Starter

    This is so cool answer. Thanks
     
    Mikestony likes this.
Loading...
Similar Threads - Why activity class
  1. puppykickr
    Replies:
    13
    Views:
    519
  2. Gary Bob
    Replies:
    11
    Views:
    762
  3. JohnJSal
    Replies:
    5
    Views:
    728
  4. AppleUser
    Replies:
    7
    Views:
    1,534
  5. Redheadattack
    Replies:
    5
    Views:
    658
  6. BuckLikesAndroid
    Replies:
    6
    Views:
    888
  7. groston
    Replies:
    6
    Views:
    1,494
  8. metroidRoid2
    Replies:
    6
    Views:
    1,892
  9. ajdroid7
    Replies:
    0
    Views:
    631
  10. iliji
    Replies:
    0
    Views:
    1,761

Share This Page

Loading...