Classes vs. Android Activities

I am new to Android development, but not programming. In any case, this question may be a little dumb.

The question is, are all classes in Android actions associated with a user interface element? I want to have a “regular” Java class from which I can usually create objects, and I won’t figure out how to “define it” and how to “name” it.

Any help would be greatly appreciated. Thanks

+4
source share
2 answers

Yes, you can have regular classes and not all of them are associated with a user interface element. This is very similar to regular Java. Thus, in Eclipse you can create a new class, as in the image, and follow the wizard of one page.

Creating a new class in Eclipse

You end up with a code similar to the one below (I added a few examples as an example):

package this.is.your.package; public class Person{ private int age; public void setAge(int _age) { age = _age; } } 

Then you can create methods and other things as usual. Regarding instantiating or accessing your class, you may have to publish it for your actions. However, there are many different ways to do this, but in the above example I could do.

 public class MyActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Person me = new Person(); me.setAge(22); //feels old } 

As you can see, all this is normal.

+9
source

The answer is NO. All actions are regular Java classes, and you can - of course, have many non-user classes, such as Application , you can have helpers, etc. If I understand your question correctly, you are confused by the fact that no user-defined constructor has activity and can only be created indirectly by calling the startActivity method, but in other cases it is a regular Java class.

+2
source

Source: https://habr.com/ru/post/1390746/


All Articles