Android Study Notes - Activity
4550 단어 Activity
An application usually consists of multiple activities, one as the main activity and can start other activities based on the user actions.
The system preserves the activities in a stack("back stack") which abides to the "last in, first out"principle. see Tasks and Back Stack . Activity uses callback methods to respond to changes in state in its lifecycle.
Creating an Activity
you must create a subclass of Activity, you need to implement callback methods. The most important methods are:
Implementing a user interface
The user interface for an activity is provided by a hierarchy of views -- objects derived from the View class. Each view controls a particular rectangular space within the activity's window(i.e. Button)
Widgets are views that are visual elements, while Layouts are views derived from ViewGroup that provide a unique layout for its child views.
The most common way to define a layout using views is with an XML layout file.
Delcaring the activity in the manifest
You must declare your activity in the manifest file making it accessible to the system. The android:name is the only required attribute that specifies the class name of the activity. An activity uses intent-filter to declare how other application may activate it.
Starting an Activity
You can activate an activity by creating an intent that explicitly defines the activity you want to start:
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);
If you want to activate an external activity to perform some actions for you, you can create the following intent:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);
startActivity(intent);
sometimes, you want to receive the result from the activity you start, you need to implement the callback method onActivityResult(), then you can get the result in an Intent in your onActivityResult method.
private void pickContact() {
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) {
Cursor cursor = getContentResolver().query(data.getData()), new String[]{Contacts.DISPLAY_NAME}, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
String name = cursor.getString(columnIndex)
// do something here
}
}
}
Shutting Down an Activity
You can shut down an activity by calling its finish() method, or shut down an activity you previously started by calling finishActivity()
Managing the Activity Lifecycle
You can manage your activity lifecycle by implementing callback methods. An activity can exists in three states:
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Native Module/React-Native바코드 리더기가 내장되어 있는 안드로이드 기기의 바코드 기능을 react-native 로 구현한 앱에서 구동시키기 위해서는 java로 작성된 코드에 엑세스 해야 했고, 이를 구현하기 위해서 native-module을...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.