1

how to create object of activity and call its life cycle method like a normal java class. for example,

In Android

class ABC extends Activity{}

new ABC().onCreate(null);
Ashan
  • 479
  • 7
  • 28
  • what do you want to do exactly? – Amy Mar 09 '16 at 07:54
  • 1
    why do you want to create object of an activity? you can start activity using an intent – Bibaswann Bandyopadhyay Mar 09 '16 at 07:55
  • Check this link hope its useful. [Link](http://stackoverflow.com/questions/14956018/can-i-create-the-object-of-a-activity-in-other-class) – Viveka Patel Mar 09 '16 at 08:01
  • ActivityThread class in android is responsible for initiating & calling activity's life cycle method depending on related events. Creating an activity object and calling its method like you mentioned will not result your class to behave like a normal activity. – Ashlesha Sharma Mar 09 '16 at 08:04
  • @BibaswannBandyopadhyay But suppose he is a newbie to Android and he is from the OOPS or java world, what would he assume? Just like anyone else he would assume to access the methods or variable of that class we will have to create an object of that class right, is there something counter intuitive with how the android was designed? – Sreekanth Karumanaghat Dec 25 '19 at 08:49
  • 1
    @SreekanthKarumanaghat You got a point. But this is how android works – Bibaswann Bandyopadhyay Jan 03 '20 at 07:14
  • @BibaswannBandyopadhyay But android coding is supposedly based on Java (at-least before Kotlin was introduced) & Java is an Object oriented Language, so mine is a very valid point I think. – Sreekanth Karumanaghat Jan 03 '20 at 09:05
  • @SreekanthKarumanaghat Yes but android also has it's separate features. Unlike java, android doesn't start execution from main(). Also android has UI components, which are created by android framework and don't require initialization – Bibaswann Bandyopadhyay Jan 06 '20 at 09:26

1 Answers1

5

If you are looking to start a new activity (like from your main activity to ABC activity), you do it this way

Intent i = new intent(this, ABC.class);
startActivity(i);

if you looking for passing data, use putExtra() method with the intent. if your data is much larger, you can always use SharedPreferences or SqlLite database, depending on your need. Hope it helps

O_o
  • 1,103
  • 11
  • 36