Doubt about Android Activity syntax and the "onCreate" method and what are its arguments!

Asked

Viewed 57 times

1

I’m new to the programming world and I’ve been studying java. I’m wanting to understand the Java application for Android apps and came across these methods from Activity.

The theory I understand without problems, but I get stuck in the syntax. Basically I need someone to explain what these terms mean and why they are arranged the way they are. For example:

protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
}

I know that protected void onCreate(Bundle savedInstanceState){ is a method; I know that Bundle and savedInstanceState are parameters/arguments of this method, know what they allegedly do which is to save information states to allow them to be recovered after some interruption in the Activity, but my doubts are:

  • What exactly is a Bundle? It is certainly a parameter for the method, but many things can be parameters. Is it a variable? Object? Class? Another method (is it possible?)
  • What it is and where it comes from Bundle and savedInstanceState?
  • What about the syntax within the scope of the other lines (onCreate and setContentview)?

I’m confused because I don’t know where these elements come from, I know they come from some imported library, but what they are or represent?

  • 2

    "Bundle and savedInstanceState are parameters" - no, there’s only one parameter there, that is the savedInstanceState. Bundle is his type (in this case, the class called Bundle).

  • 1

    Study Java first. Then go to Android. I don’t even know if I recommend an "Objects first" approach to learning programming. Perhaps it would be better to study a simpler paradigm first, a procedural one, perhaps a functional one, a Python, a Javascript, but focusing on procedural and programming logic. It’s hard, we want to teach to study right, and not to turn "just another half-assed programmer" as there are many around (I include myself in this).

  • Well... I don’t think object orientation is out of this world, I have procedural programming notions, I am not so layman so and have learned a lot of things relatively quickly and even faster than I imagined I would learn... some syntax implementations leave me in doubt as if it is possible to put classes as parameters of methods and what are certain parameters in relation to those classes, things that if answered will solve 70% of certain comprehension problems that I currently have.

  • 1

    Object orientation the theory seems simple, but it well done is complicated to learn, it takes time. And poorly done it looks cute at first but only increases complexity in the medium and long term, Reweights the code, causes bad abstraction and excessive involvement, because bad design decisions accumulate. Hence the recommendation to take the procedural first. If you are studying Java, should not have these doubts of class and object on Android, only doubts specific to Android.

  • 1

    If you have these questions, I suggest you learn how to use Stack Overflow. Search for example difference+class+object

  • Got it... but says ai... if Bundle is a class and it is possible to put it as parameter to another method it means that it is possible to import the attributes, variables and even internal methods of this class within the scope of the method that receives it as parameter? and as for the savedInstanceState, it is what within the Bundle?

  • 1

    savedInstanceState is one Bundle (to be more precise, it is an instance of the class Bundle). As @Piovezan has already said - and I’m sorry if I sound rude, but it’s to help you - first you should study Java, because it seems like you’re having trouble understanding the basics of the language (for example, difference between class and class instance, which are parameters of a method, etc.). After understanding how the language works, there you go pro Android, to understand how/when/ why each method is called in each situation,

  • I was going to try to explain it properly but I curled up, the @hkotsubo passed in front of me. Difference between parameter and argument. It’s like he said. Look for a good material on Java, in a quick reminder I can suggest for example the Caelum, which is usually well spoken, but I did not look at the material in depth: https://www.caelum.com.br/apostila/apostila-java-oriented-objects.pdf if you know English I prefer Oracle tutorials (The Java Tutorials). Videos I don’t know.

  • Yes, these are basic doubts. When I came across the programming of android I realized the shock of structuring difference in relation to the pc, even being java and also object orientation. I also had already thought of returning to focus on java and leave android as secondary, but I was in doubt if I was just being cowardly or inventing excuse in my mind to simply not study.

  • Programming is a lot of fun. I don’t want to linger. The Internet has a lot of material but a lot is not saved, and the best is in English, you have to filter what you will study to learn correctly and not have to "unlearn" the wrong afterwards. You have a new brain, soaps... I mean, spacious kkkk, you have to take advantage that you are young to firm up correct concepts, form the basis. Try to study, take a good course in the area, if possible, if that’s what you want to do.

  • Anyway. I suggest you research something like "How to learn to program the right way" and read some of the answers you find.

Show 7 more comments

1 answer

2

Bundle is a class whose function is to save and retrieve state information about the Activity in question. There are certain situations, such as changing the orientation of the apparatus (portrait p/ landscape, for example), in which the Activity needs to be reconstructed and her previous state recovered, in which case, the onCreate is called and, in the Bundle, will be the information of Activity so that she can be recreated the way she was before the event that called this onCreate.

The setContentView basically "draws" the Activity. It receives as parameter a reference to an XML file that represents the "drawing", the graphical schematization of what will be displayed to the user. In other words, each "screen" of an app is one Activity, built from an XML file.

  • Yes. You spoke very well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.