Android Activity Part 1

Adopt from

[1] http://developer.android.com/guide/topics/fundamentals.html


What is Activity?

From our daily experience, we need to perform some activities before we can finish a job. For instance, a dinner, you need to go to the supermarket, choose the ingredients, bring them home and cook them all. An activity composes of activities and the basic unit is the action.

In Android, the idea of activity restricts on a task completed on the screen. An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture. [1]

How to implement Activities?

 

In simplest case, your activity needs to inherit from Activity. To create your activity, you need to override to onCreate method. Then, you need to put your view (technically, pass the reference of your view) in to the activity as the definition highlighted above.

public class MyActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        TextView textview = new TextView(this); // Create the view object within the activity class
        textview.setText("This is the a tab");
        setContentView(textview);
    }
}

public class MyActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        TextView textview = (TextView)findViewById(R.id.textview); // Get the reference from project resource (the layout is defined in XML)
        textview.setText("This is the a tab");
        setContentView(textview);
    }
}

本篇發表於 Android Apps Dev Competition, Programming 並標籤為 , 。將永久鏈結加入書籤。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *