• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Could someone please explain the activity Android class to me?

m130

Lurker
I'm new to Android, I have Java experience. I'm a bit confused on the activity it automatically manufactures for you: I get that it pulls information from the R.java file, but could someone break down the (in this case) "Main Method" for me? I would truly appreciate it; I really do like Android.
I'll post it below to assure we are talking about the same thing.
Code:
package org.example.sudoku;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class Sudoku extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sudoku);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sudoku, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_sudoku, container, false);
            return rootView;
        }
    }

}

Bonus Question: Why do all these methods have "on" in front of them?

Thanks
 
For each of the state in the Activity Lifecycle, there is a method which you
can think of as the "Main Method" for that specific state. In order to customize
the behaviour of your activity in a specific state you need to override the
corresponding method. Read more about the Activity Lifecycle.

The first auto-generated method is the onCreate() method which overrides the default
behaviour in the onCreate() state of the activity. The method does the following:
1. Calls the onCreate() method of parent class.
2. Sets the view using the setContentView() method and providing the XML layout file.
3. If the activity is created for the first time (savedInstanceState == null), it initializes
the view with a new Fragment.
The PlaceholderFragment class is a dummy fragment class which should be replaced.
Note that the PlaceholderFragment class is declared as a static internal class for avoiding leaks.

The next 2 methods are not Lifecycle methods. They are related to the creation of the
options menu (onCreateOptionsMenu), and the handling of option items selections.
I recommend that you read more about options menus.

You should be aware that your Sudoku class does not extend the Activity class
directly. It extends the ActionBarActivity class,
which enables using of the ActionBar while supporting API levels lower than 11.
 
For each of the state in the Activity Lifecycle, there is a method which you
can think of as the "Main Method" for that specific state. In order to customize
the behaviour of your activity in a specific state you need to override the
corresponding method. Read more about the Activity Lifecycle.

The first auto-generated method is the onCreate() method which overrides the default
behaviour in the onCreate() state of the activity. The method does the following:
1. Calls the onCreate() method of parent class.
2. Sets the view using the setContentView() method and providing the XML layout file.
3. If the activity is created for the first time (savedInstanceState == null), it initializes
the view with a new Fragment.
The PlaceholderFragment class is a dummy fragment class which should be replaced.
Note that the PlaceholderFragment class is declared as a static internal class for avoiding leaks.

The next 2 methods are not Lifecycle methods. They are related to the creation of the
options menu (onCreateOptionsMenu), and the handling of option items selections.
I recommend that you read more about options menus.

You should be aware that your Sudoku class does not extend the Activity class
directly. It extends the ActionBarActivity class,
which enables using of the ActionBar while supporting API levels lower than 11.

That explanation was ridiculously good! Will check out the site. Thanks!
 
Back
Top Bottom