`
sillycat
  • 浏览: 2542001 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Android UI(1)Getting Started - First App and Manage Activities

 
阅读更多
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

分享到:
评论

相关推荐

    Android-android-ui-animation-components-and-libraries.zip

    Android-android-ui-animation-components-and-libraries.zip,android ui库、组件和动画作者@ramotion-https://github.com/ramotion/swift-ui-animation-components-libraries,安卓系统是谷歌在2008年设计和制造的。...

    基于uni-app开发的跨平台移动端UI 组件库兼容App-Nvue、App-vue、小程序

    First UI 是一套基于uni-app开发的组件化、可复用、易扩展、低耦合的跨平台移动端UI 组件库。全面兼容App-Nvue、App-vue、小程序(微信、支付宝、百度、字节、QQ)、H5。文档地址:.zip

    uni-app+color-ui+uview-ui 搭建的H5端初始demo

    1. **uni-app**:uni-app 是由ECharts团队开发的一个跨端框架,允许开发者编写一次代码,可以同时在iOS、Android、Web(H5)、微信小程序、支付宝小程序、百度小程序等多个平台运行。它基于Vue.js,提供了丰富的组件...

    Android-My-First-app

    在Android开发领域,"Android-My-First-app"通常指的是初学者创建的第一个应用程序项目,它是一个基础教程,帮助开发者理解Android应用的基本结构和工作原理。这个项目主要使用Java语言编写,因为Java是Android官方...

    Android-firstApp

    总的来说,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【Android 前台】66828新UI新版JAVA原生双端影视APP投屏影视APP源码_1...

    Android UI设计

    Android UI设计内容简介: 创造一个统一外观,感觉完整的用户界面会增加你的产品附加价值。 精炼的图形风格也使用户觉得用户界面更加专业。 帮助你如何在应用界面的不同部分创造图标来匹配 Android 2.x框架下的普遍...

    restclient-ui-3.2.2-jar-with-dependencies

    "restclient-ui-3.2.2-jar-with-dependencies" 是这个工具的一个特定版本,该版本包含了所有必要的依赖项,使得用户可以直接运行而无需额外安装其他库。这个版本号表明它是RESTClient的3.2.2迭代,且“jar-with-...

    Android-WiseOwl-FirstApp

    【Android-WiseOwl-FirstApp】项目是一个基于Kotlin语言的Android开发教程,旨在帮助初学者构建他们的第一个Android应用程序。这个项目的核心是通过实践学习Android应用开发的基础知识,特别是利用Kotlin编程语言来...

    Android代码-android-mvvm-sample-app

    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 ...

    android-整体UI设计-滑动导航栏+滚动页面

    很多朋友对RollNavigationBar+SlidePageView如何设计业务界面感到疑惑,今天我专门写...了解详情可以看我的博客《android-整体UI设计-(滑动导航栏+滚动页面)》http://blog.csdn.net/swadair/article/details/7551609

    Android UI Design by Jessica Thornsby , 0分

    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-app-master 源代码

    【Android App Master 源代码】是一个开源项目,旨在提供Android应用开发的示例和学习资源。这个项目可能包含了各种Android应用开发的关键组件和技术,帮助开发者深入理解Android平台的工作原理和最佳实践。以下是对...

    android-async-http-1.4.8.jar

    强大的网络请求库,主要特征如下: 处理异步Http请求,并通过匿名内部类...Http请求均位于非UI线程,不会阻塞UI操作 通过线程池处理并发请求 处理文件上传、下载 响应结果自动打包JSON格式 自动处理连接断开时请求重连

    Android机器人工作室Ribot-app-android.zip

    Ribot-app-android是 http://ribot.co.uk/ (一个UI设计工作室)的官方APPAPP功能:登录 - 使用@ ribot.co.uk谷歌帐户登录自动签到 - 通过Estimote beacons设备自动签到手动登录 - 手动签到团队列表 - 查看Ribot团队...

    新版Android开发教程及笔记-完整版.pdf

    新版Android开发教程+笔记七--基础UI编程1.pdf 新版Android开发教程+笔记八--基础UI编程2.pdf 新版Android开发教程+笔记九--基础UI编程3.pdf 新版Android开发教程+笔记十--基础UI编程4.pdf 新版Android开发教程+笔记...

    my-first-app:我的第一个Android应用程序(Android Studio教程)

    在本教程中,我们将一起探索如何使用Android Studio创建一个名为"my-first-app"的基本Android应用程序。Android Studio是Google为开发Android应用提供的官方集成开发环境(IDE),它提供了丰富的功能和工具,让...

    org.eclipse.paho.ui.app-1.0.2-win32.win32.x86_64.zip

    《MQTT测试工具——org.eclipse.paho.ui.app深入解析》 在物联网(IoT)领域,MQTT(Message Queuing Telemetry Transport)协议因其轻量级、高效的特点,被广泛应用于设备间的通信。为了便于开发者进行MQTT协议的...

    android-support-v7-appcompat.jar android-support-v4.jar

    在Android开发中,`android-support-v7-appcompat.jar` 和 `android-support-v4.jar` 是两个非常重要的库文件,它们提供了对旧版本Android系统的重要支持和功能扩展。 首先,`android-support-v7-appcompat.jar` 是...

    20.[开源][安卓][翻页效果的UI组件]android-flip-master

    20.[开源][安卓][翻页效果的UI组件]android-flip-master Aphid FlipView是一个能够实现Flipboard翻页效果的UI组件。

Global site tag (gtag.js) - Google Analytics