- 浏览: 2551091 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Android UI(1)Getting Started - First App and Manage Activities
1. Build Your First App
Creating an Android Project
Minimum Required SDK - is the lowest version of Android that our app supports.
Target SDK - indicates the highest version of Android(also using the API level) with which you have tested with your app. As new versions of Android become available, you should test your app on the new version and update this value to match the latest API level in order to take advantage of new platform features.
Compile With - By default, this is set to the latest version of Android available in my SDK.
Theme
Build a Simple User Interface
View and ViewGroup objects.
View objects are usually UI widgets such as buttons or text fields.
ViewGroup objects are invisible view containers that define how the child views are laid out.
Create a Linear Layout
LinearLayout is a view group(a subclass of ViewGroup).
Layout in details
http://developer.android.com/guide/topics/ui/declaring-layout.html
Add a Text Field
EditText -
android:id This provides a unique identifier for the view, which you can use to reference the object from your app code.
Add String Resources
<resources> in the strings.xml
Add a Button
<Button>
android:text = "@string/button_get_json" refer the configuration to a string resource.
Respond to the Send Button
<Button>
android:onClick="sendMessage"
There should be a method in activity
public void sendMessage(View view){
…snip...
}
But in my example, EasyRestClientAndroid project, I bind the action to the button as follow:
final button buttonJson = (Button)findViewById(R.id.button_json);
buttonJson.setOnClickListener(new View.OnClickListener() {
publicvoid onClick(View v) {
new DownloadStateTask().execute(MediaType.APPLICATION_JSON);
}
});
Build an Intent
Intent is an object that provides runtime binding between separate components(such as 2 activities).
Intent will most often be used to start another activity.
Intent intent = new Intent();
intent.setClass(this, PersonListActivity.class);
startActivity(intent);
And we can also send message in intent
public final static String EXTRA_MESSAGE = "com.sillycat.easyresclienttandroid.message"
intent.putExtra(EXTRA_MESSAGE, message);
This method put the first parameter as the key, the second as the value.
Start the Second Activity
Send the intent as follow
startActivity(intent);
The System will receives this call and starts an instance of the Activity.
And the title string
<resources>
<string name="title_activity_main">EasyRestClientAndroid</string>
…snip…
In the AndroidManifest.xml
<application
android:name=".mainframe.MainApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"]]>
<activity
android:name=".mainframe.MainActivity"
android:label="@string/title_activity_main">
Receive the Intent
Every Activity in invoked by an Intent, we can get that object through getIntent()
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
2. Managing the Activity Lifecycle
Download and get the file ActivityLifecycle.zip. Unzip the file. Import the project through 'Android Project from Existing Code'
Activities - foreground, background
Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. The activity will not consume system resources when I did not need them.
Starting an Activity
Understand the Lifecycle Callbacks
As the system creates a new activity instance, each callback method moves the activity state one step toward the top.
onCreate() ----> onStart() ----> onResume() --- onPause() -----> onStop ----> onRestart() -----> onDestroy()
State - Resumed, Paused, Stopped
Specify Your App's Launcher Activity
When the user selects your app icon from the Home Screen, the system calls the onCreate() method for the Activity in your app that I declared to be the "launcher" (or "main") activity.
That is the definition of Launcher Activity.
<activity
android:name=".mainframe.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And we can register all the other activities in the <application>
Create a New Instance
The system creates every new instance of Activity by calling its onCreate() method. Usually, the onCreate() should define the user interface and possibly instantiate some class-scope variables.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
Check the version before we input the new codes.
Once we onCreate(), we will move on to onStart() ----> onResume()
Once other activity called, the old activity will onPause() ----> onStop()
Once we click the original activity, it will onRestart() ----> onStart() -----> onResume(), and the newly created activity B will
onPause() --> onStop() ----> onDestroy()
Destroy the Activity
The system will call onDestroy() that my instance is being completely removed from the system memory.
Pausing and Resuming an Activity
When a diagram show up, the original activity will onPause() and stay on paused. In this situation, we can also see the activity. If it is invisible, the activity should be stopped.
Pause Your Activity
Mostly, we need to do these things on onPause()
- Stop animations or other ongoing actions that could consume CPU.
- Save the draft email, something like that.
- Release system resources, such as broadcast receivers, handles to sensors(like GPS).
Example to release the camera
public void onPause(){
super.onPause();
if(mCamera != null){
mCamera.release();
mCamera = null;
}
}
Resume Your Activity
Every time we start, we call the onResume() method. We need to initialize components that we release during onPause().
Example
public void onResume(){
super.onResume();
if(mCamera == null){
initializeCamera();
}
}
Stopping and Restarting an Activity
Stop Your Activity
Start/Restart Your Activity
…snip…
Recreating an Activity
The system may destroy the activity if the user press Back or the activity finishes itself, the system's concept of that Activity instance is gone forever.
We will use an object named Bundle to store the state.
By default, the system uses the Bundle instance state to save information about each View object.
To save additional data about the activity state, we need to override the onSaveInstanceState() callback method.
Resumed ------> onSaveInstanceState() ------> Destroyed
onCreate/Created--> onRestoreInstanceState() ---> Resumed
Save Your Activity State
For instance as follow:
static final String STATE_SCORE = "playerScore"
public void onSaveInstanceStore(Bundle savedInstanceState){
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
super.onSaveInstanceState(savedInstanceState);
}
Restore Your Activity State
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(savedInstanceState != null){
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
}
}
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
…same as onCreate...
}
References:
http://developer.android.com/training/basics/firstapp/creating-project.html
http://developer.android.com/training/basics/activity-lifecycle/starting.html
1. Build Your First App
Creating an Android Project
Minimum Required SDK - is the lowest version of Android that our app supports.
Target SDK - indicates the highest version of Android(also using the API level) with which you have tested with your app. As new versions of Android become available, you should test your app on the new version and update this value to match the latest API level in order to take advantage of new platform features.
Compile With - By default, this is set to the latest version of Android available in my SDK.
Theme
Build a Simple User Interface
View and ViewGroup objects.
View objects are usually UI widgets such as buttons or text fields.
ViewGroup objects are invisible view containers that define how the child views are laid out.
Create a Linear Layout
LinearLayout is a view group(a subclass of ViewGroup).
Layout in details
http://developer.android.com/guide/topics/ui/declaring-layout.html
Add a Text Field
EditText -
android:id This provides a unique identifier for the view, which you can use to reference the object from your app code.
Add String Resources
<resources> in the strings.xml
Add a Button
<Button>
android:text = "@string/button_get_json" refer the configuration to a string resource.
Respond to the Send Button
<Button>
android:onClick="sendMessage"
There should be a method in activity
public void sendMessage(View view){
…snip...
}
But in my example, EasyRestClientAndroid project, I bind the action to the button as follow:
final button buttonJson = (Button)findViewById(R.id.button_json);
buttonJson.setOnClickListener(new View.OnClickListener() {
publicvoid onClick(View v) {
new DownloadStateTask().execute(MediaType.APPLICATION_JSON);
}
});
Build an Intent
Intent is an object that provides runtime binding between separate components(such as 2 activities).
Intent will most often be used to start another activity.
Intent intent = new Intent();
intent.setClass(this, PersonListActivity.class);
startActivity(intent);
And we can also send message in intent
public final static String EXTRA_MESSAGE = "com.sillycat.easyresclienttandroid.message"
intent.putExtra(EXTRA_MESSAGE, message);
This method put the first parameter as the key, the second as the value.
Start the Second Activity
Send the intent as follow
startActivity(intent);
The System will receives this call and starts an instance of the Activity.
And the title string
<resources>
<string name="title_activity_main">EasyRestClientAndroid</string>
…snip…
In the AndroidManifest.xml
<application
android:name=".mainframe.MainApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"]]>
<activity
android:name=".mainframe.MainActivity"
android:label="@string/title_activity_main">
Receive the Intent
Every Activity in invoked by an Intent, we can get that object through getIntent()
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
2. Managing the Activity Lifecycle
Download and get the file ActivityLifecycle.zip. Unzip the file. Import the project through 'Android Project from Existing Code'
Activities - foreground, background
Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. The activity will not consume system resources when I did not need them.
Starting an Activity
Understand the Lifecycle Callbacks
As the system creates a new activity instance, each callback method moves the activity state one step toward the top.
onCreate() ----> onStart() ----> onResume() --- onPause() -----> onStop ----> onRestart() -----> onDestroy()
State - Resumed, Paused, Stopped
Specify Your App's Launcher Activity
When the user selects your app icon from the Home Screen, the system calls the onCreate() method for the Activity in your app that I declared to be the "launcher" (or "main") activity.
That is the definition of Launcher Activity.
<activity
android:name=".mainframe.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And we can register all the other activities in the <application>
Create a New Instance
The system creates every new instance of Activity by calling its onCreate() method. Usually, the onCreate() should define the user interface and possibly instantiate some class-scope variables.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
Check the version before we input the new codes.
Once we onCreate(), we will move on to onStart() ----> onResume()
Once other activity called, the old activity will onPause() ----> onStop()
Once we click the original activity, it will onRestart() ----> onStart() -----> onResume(), and the newly created activity B will
onPause() --> onStop() ----> onDestroy()
Destroy the Activity
The system will call onDestroy() that my instance is being completely removed from the system memory.
Pausing and Resuming an Activity
When a diagram show up, the original activity will onPause() and stay on paused. In this situation, we can also see the activity. If it is invisible, the activity should be stopped.
Pause Your Activity
Mostly, we need to do these things on onPause()
- Stop animations or other ongoing actions that could consume CPU.
- Save the draft email, something like that.
- Release system resources, such as broadcast receivers, handles to sensors(like GPS).
Example to release the camera
public void onPause(){
super.onPause();
if(mCamera != null){
mCamera.release();
mCamera = null;
}
}
Resume Your Activity
Every time we start, we call the onResume() method. We need to initialize components that we release during onPause().
Example
public void onResume(){
super.onResume();
if(mCamera == null){
initializeCamera();
}
}
Stopping and Restarting an Activity
Stop Your Activity
Start/Restart Your Activity
…snip…
Recreating an Activity
The system may destroy the activity if the user press Back or the activity finishes itself, the system's concept of that Activity instance is gone forever.
We will use an object named Bundle to store the state.
By default, the system uses the Bundle instance state to save information about each View object.
To save additional data about the activity state, we need to override the onSaveInstanceState() callback method.
Resumed ------> onSaveInstanceState() ------> Destroyed
onCreate/Created--> onRestoreInstanceState() ---> Resumed
Save Your Activity State
For instance as follow:
static final String STATE_SCORE = "playerScore"
public void onSaveInstanceStore(Bundle savedInstanceState){
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
super.onSaveInstanceState(savedInstanceState);
}
Restore Your Activity State
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(savedInstanceState != null){
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
}
}
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
…same as onCreate...
}
References:
http://developer.android.com/training/basics/firstapp/creating-project.html
http://developer.android.com/training/basics/activity-lifecycle/starting.html
发表评论
-
ionic UI(4)ionic2 framework - basic and components and native
2016-03-24 02:33 1261ionic UI(4)ionic2 framework - b ... -
ionic UI(3)TypeScript - handbook
2016-03-22 23:21 634ionic UI(3)TypeScript - handboo ... -
ionic UI(2)ionic2 framework - TypeScript - tutorial
2016-03-22 06:52 1656ionic UI(2)ionic2 framework - T ... -
Parse and Heroku Service(3)Parse Server and Parse Dashboard
2016-03-22 06:30 967Parse and Heroku Service(3)Pars ... -
Parse and Heroku Service(2)Mail Templates and Push Notification
2016-03-22 02:45 583Parse and Heroku Service(2)Mail ... -
ionic UI(1)Introduction
2016-03-19 03:18 723ionic UI(1)Introduction 1 Inst ... -
Parse and Heroku Service(1)Heroku Installation and Play
2016-03-19 00:13 824Parse and Heroic Service(1)Hero ... -
Hybrid(5)Customize Meteor Directly Google Login
2015-09-01 02:33 910Hybrid(5)Customize Meteor Direc ... -
Hybrid(4)Favorite Places - Google Login
2015-09-01 02:02 1341Hybrid(4)Favorite Places - Goog ... -
Hybrid(3)More Meteor Example - Social
2015-08-11 05:04 756Hybrid(3)More Meteor Example - ... -
Hybrid(2)meteor Running Android and iOS
2015-07-28 23:59 1049Hybrid(2)meteor Running Android ... -
Create the Google Play Account
2015-07-18 06:42 1101Create the Google Play Account ... -
Secure REST API and Mobile(1)Document Read and Understand OAUTH2
2015-07-14 00:36 764Secure REST API and Mobile(1)Do ... -
Screen Size and Web Design
2015-07-11 01:11 725Screen Size and Web Design iPh ... -
Hybrid(1)ionic Cordova meteor
2015-06-25 05:49 471Hybrid(1)ionic Cordova meteor ... -
Android Fire Project(1)Recall Env and Knowledge
2015-02-11 12:28 686Android Fire Project(1)Recall ... -
Android Content Framework(1)Concept
2014-06-14 13:54 1080Android Content Framework(1)Con ... -
Feel Android Studio(1)Install and Update Android Studio
2014-04-11 03:12 2029Feel Android Studio(1)Install a ... -
IOS7 App Development Essentials(2)iBeacon
2014-03-05 05:55 890IOS7 App Development Essentials ... -
IOS7 App Development Essentials(1) Persistent Store
2014-03-05 05:54 1327IOS7 App Development Essentials ...
相关推荐
•Android---UI篇---ListView之SampleAdapter(列表)---1 • •Android---UI篇---ListView之ArrayAdapter(列表)---2 • •Android---UI篇---ListView之SimpleCursorAdapter(列表)---3 • •Android---UI篇---...
First UI 是一套基于uni-app开发的组件化、可复用、易扩展、低耦合的跨平台移动端UI 组件库。全面兼容App-Nvue、App-vue、小程序(微信、支付宝、百度、字节、QQ)、H5。文档地址:.zip
在Android开发领域,"Android-My-First-app"通常指的是初学者创建的第一个应用程序项目,它是一个基础教程,帮助开发者理解Android应用的基本结构和工作原理。这个项目主要使用Java语言编写,因为Java是Android官方...
This project includes sample applications that demonstrate different features of the Android Framework and Support Library. It is actively managed by the Android Framework Team. Disclaimers This is ...
总的来说,Android-firstApp项目涵盖了Android应用开发的基础,包括Android Studio的使用、Java编程、UI设计、组件交互、资源管理和测试发布等,是一个很好的起点,帮助开发者入门Android世界。通过这个项目,你可以...
【Android 前台】66828新UI新版JAVA原生双端影视APP投屏影视APP源码_1【Android 前台】66828新UI新版JAVA原生双端影视APP投屏影视APP源码_1【Android 前台】66828新UI新版JAVA原生双端影视APP投屏影视APP源码_1...
"restclient-ui-3.2.2-jar-with-dependencies" 是这个工具的一个特定版本,该版本包含了所有必要的依赖项,使得用户可以直接运行而无需额外安装其他库。这个版本号表明它是RESTClient的3.2.2迭代,且“jar-with-...
【Android-WiseOwl-FirstApp】项目是一个基于Kotlin语言的Android开发教程,旨在帮助初学者构建他们的第一个Android应用程序。这个项目的核心是通过实践学习Android应用开发的基础知识,特别是利用Kotlin编程语言来...
A basic sample android application to understand MVVM in a very simple way. The app has following packages: data: It contains all the data accessing and manipulating components. ui: View classes ...
很多朋友对RollNavigationBar+SlidePageView如何设计业务界面感到疑惑,今天我专门写...了解详情可以看我的博客《android-整体UI设计-(滑动导航栏+滚动页面)》http://blog.csdn.net/swadair/article/details/7551609
First UI 是一套基于uni-app开发的组件化、可复用、易扩展、低耦合的跨平台移动端UI 组件库。全面兼容App-Nvue、App-vue、小程序(微信、支付宝、百度、字节、QQ)、H5。
强大的网络请求库,主要特征如下: 处理异步Http请求,并通过匿名内部类...Http请求均位于非UI线程,不会阻塞UI操作 通过线程池处理并发请求 处理文件上传、下载 响应结果自动打包JSON格式 自动处理连接断开时请求重连
Android UI Design:Plan, design, and build engaging user interfaces ... Start getting to grips with the new UI features coming up in Android N, including multi-window mode and direct reply notifications
新版Android开发教程+笔记七--基础UI编程1.pdf 新版Android开发教程+笔记八--基础UI编程2.pdf 新版Android开发教程+笔记九--基础UI编程3.pdf 新版Android开发教程+笔记十--基础UI编程4.pdf 新版Android开发教程+笔记...
在本教程中,我们将一起探索如何使用Android Studio创建一个名为"my-first-app"的基本Android应用程序。Android Studio是Google为开发Android应用提供的官方集成开发环境(IDE),它提供了丰富的功能和工具,让...
在移动应用开发领域,uni-app是一个备受青睐的跨平台框架,它允许开发者用一套代码实现多端部署,包括iOS、Android、Web等。本实例将详细讲解如何使用HBuilderX工具来开发一个uni-app登录功能。 首先,我们需要了解...
jquery插件jquery-ui-timepicker-addon.j
在Android平台上,由于UI线程不能长时间执行耗时操作,因此异步网络请求显得尤为重要,它可以确保用户界面的流畅性。 该库的核心类`AsyncHttpClient`提供了丰富的API,允许开发者轻松发起GET、POST等HTTP请求。例如...
在Android开发领域,"my-first-app"通常是指开发者创建的第一个示例程序,它标志着学习和探索Android编程的开始。这个项目可能是一个简单的“Hello, World!”应用,旨在帮助初学者理解基本的Android应用架构和组件。...
This includes consistent and meaningful use of Material Design UI components, fonts, color, motion and surfaces. License Copyright 2016 Dmitry Malkovich Licensed under the Apache License, Version ...