Our first app

knapsackyarnMobile - sans fil

14 déc. 2013 (il y a 3 années et 5 mois)

72 vue(s)

Our  first  app  
Key  terminology  
High-­‐level  behavior  


Any  applica:on  can  start  another  applica:on’s  component  


For  example,  one  can  simply  start  the  ac:vity  in  the  camera  
applica:on  that  captures  a  photo.  When  complete,  the  photo  is  even  
returned  to  your  applica:on  so  you  can  use  it  


When  the  system  starts  a  component,  it  starts  the  process  for  that  
applica:on  and  instan:ates  the  classes  needed  for  the  component  


For  example,  if  one  starts  the  ac:vity  in  the  camera  applica:on  that  
captures  a  photo,  that  ac:vity  runs  in  the  process  that  belongs  to  the  
camera  applica:on,  not  in  your  applica:on's  process  


An  applica:on  cannot  directly  ac:vate  a  component  from  another  
applica:on,  but  the  Android  system  can  


To  ac:vate  a  component  in  another  applica:on,  you  must  deliver  a  
message  to  the  system  that  specifies  your  intent  to  start  a  par:cular  
component  


The  system  then  ac:vates  the  component  for  you  
Android  through  MVC  
View  
View  and  
related  
classes  
Control  
Ac:vity  
Service  
Broadcast  
receiver  
Model  
SQLite  
Files  and  
content  
providers  
User  interface  


built  using  a  hierarchy  of  


View
 objects  are  usually  UI  widgets  such  as  
buPons  or  text  fields  


ViewGroup
 objects  are  invisible  view  containers  
(as  grids  or  ver:cal  lists)  
An  example  
ac:vity_main.xml
 
<
LinearLayout

xmlns:android
=
"http://
schemas.android.com
/
apk
/res/android"

xmlns:tools
=
"http://
schemas.android.com
/tools"

android:layout_width
=
"
match_parent
"

android:layout_height
=
"
match_parent
"

android:orientation
=
"horizontal"
>

<
EditText

android:id
=
"@+id/
edit_message
"

android:layout_weight
=
"1"

android:layout_width
=
"0dp"

android:layout_height
=
"
wrap_content
"

android:hint
=
"@string/
edit_message
"
/>

<
Button

android:layout_width
=
"
wrap_content
"

android:layout_height
=
"
wrap_content
"

android:text
=
"@
string
/
button_send
"
/>
</
LinearLayout
>
 
strings.xml
 
<
resources
>

<
string
name
=
"
app_name
"
>
My First App
</
string
>

<
string
name
=
"
edit_message
"
>
Enter a message
</
string
>

<
string
name
=
"
button_send
"
>
Send
</
string
>

<
string
name
=
"
menu_settings
"
>
Settings
</
string
>

<
string
name
=
"
title_activity_main
"
>
MainActivity
</
string
>
</
resources
>
 
…  eventually  
public

class
Main
extends
Activity {


@Override

protected

void

onCreate
(Bundle
savedInstanceState
) {

super
.onCreate
(
savedInstanceState
);

setContentView
(
R.layout.
activity_main
);
}



@Override

public

boolean

onCreateOptionsMenu
(Menu menu) {

// Inflate the menu; this adds items to the action bar

getMenuInflater
().inflate(
R.menu.
main
, menu);

return

true
;
}

}
 
Ac:vi:es  


Each  ac:vity  can  start  another  ac:vity  


Each  :me  a  new  ac:vity  starts,  the  previous  ac:vity  is  
stopped    


The  system  preserves  the  ac:vi:es  in  a  LIFO  stack  


The  new  ac:vity  is  pushed  on  top  of  the  stack  and  takes  
the  focus    
SendMessage
 
<
Button

android:layout_width
=
"
wrap_content
"

android:layout_height
=
"
wrap_content
"

android:text
=
"@string/
button_send
"

android:onClick
=
"
sendMessage
"
/>
 
public

class
Main
extends
Activity {

public

final

static
String
EXTRA_MESSAGE
=

"
com.example.myfirstapp.MESSAGE
"
;


public

void

sendMessage
(View view) {
Intent intent =
new
Intent(
this
,
DisplayMess.
class
);


EditText

eText
= (
EditText
)
findViewById
(
R.id.
edit_message
);

String message =
editText.getText
().
toString
();


intent.putExtra
(
EXTRA_MESSAGE
, message);


startActivity
(intent);
}
 
Manifest  
<
activity

android:name
=
"
luc.polimi.first.Main
"

android:label
=
"@string/
app_name
"
>

<
intent-filter
>

<
action
android:name
=
"
android.intent.action.MAIN
"
/>


<
category
android:name
=
"
android.intent.category.LAUNCHER
"
/>

</
intent-filter
>
</
activity
>
<
activity

android:name
=
"
luc.polimi.first.DisplayMess
"

android:label
=
"@string/
title_activity_display_mess
"

android:parentActivityName
=
"Main"
>

<
meta-data

android:name
=
"
android.support.PARENT_ACTIVITY
"

android:value
=
"Main"
/>
</
activity
>
 
Another  ac:vity  
<
RelativeLayout

xmlns:android
=

"http://
schemas.android.com
/
apk
/res/android"

xmlns:tools
=
"http://
schemas.android.com
/tools"

android:layout_width
=
"
match_parent
"

android:layout_height
=
"
match_parent
"
>


<
TextView


android:layout_width
=
"
wrap_content
"

android:layout_height
=
"
wrap_content
"

android:layout_centerHorizontal
=
"true"

android:layout_centerVertical
=
"true"

android:text
=
"@string/
hello_world
"

tools:context
=
".
DisplayMessageActivity
"
/>

</
RelativeLayout
>
 
onCreate
 
public

void

onCreate
(Bundle
savedInstanceState
) {

super
.onCreate
(
savedInstanceState
);


// Get the message from the intent

Intent intent =
getIntent
();

String message =
intent.getStringExtra
(
Main.
EXTRA_MESSAGE
);


// Create the text view

TextView

textView
=
new

TextView
(
this
);

textView.setTextSize
(40);

textView.setText
(message);


// Set the text view as the activity layout

setContentView
(
textView
);
}
 
Ac:vity  lifecycle