Beruflich Dokumente
Kultur Dokumente
import android.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
String tag = “Lifecycle Step”
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, “In the onCreate() event”);
}
public void onStart()
{
super.onStart();
Log.d(tag, “In the onStart() event”);
}
public void onRestart()
{
super.onRestart();
Log.d(tag, “In the onRestart() event”);
}
public void onResume()
{
super.onResume();
Log.d(tag, “In the onResume() event”);
}
public void onPause()
{
super.onPause();
Log.d(tag, “In the onPause() event”);
}
public void onStop()
{
super.onStop();
Log.d(tag, “In the onStop() event”);
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag, “In the onDestroy() event”);
}
3. Press Shift + F9 or select Run -> Debug. Then select one of your Android devices from the pop-up
menu. Notice what is displayed in the logcat console. (If not visible, click Android Monitor at the
bottom of the Android window.)
Copy here: ______________________________________________________________
1
4. Click the Back button on your Android emulator. Notice what is displayed in the logcat console.
5. Click the Home button. Notice what is displayed in the logcat console.
6. Click the Home button and then click the Phone button on the Android emulator so that the
activity is pushed to the background. Observe the output in the logcat window.
7. Exit the phone dialer by clicking the Back button. Observe the output in the logcat window.
What is the sequence of the methods that were called?
Answer:
Give your conclusion that summarizes the effects of the methods when apps are executed:
OnCreate(), onResume(), onPause(), onDestroy()
Answer:
<application…
android:theme=“@android:stlye/Theme.Material”
…
setContentView(R.layout.activity_main);
requestWindowFeature(Window.FEATURE_NO_TITLE);
…
then change the theme in the AndroidManifest.xml to a theme that has no title bar:
android:theme=“@android:style/Theme.NoTitleBar”
2
Activity #2: Dialog Project
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".DialogActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
{ @Override
3
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will automatically handle clicks on
the Home/Up button, so long as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item); } }
How it Works
Android uses the AppCompat.Dialog theme to draw your standard activity as a free-floating dialog box.
1. Open a new project. Name it ProgressDialog Make sure that you are using the Material theme in
the AndroidManifest.xml file.
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.Material" >
<activity android:name=".MainActivity"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
2. Add the bolded statements from the following code to the MainActivity.java file:
3. import android.app.Activity;
import android.app.ProgressDialog;
import android.os.CountdownTimer;
import android.os.Bundle;
public class MainActivity extends Activity {
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
4
{ @Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
progressDialog.dismiss();
}
} .start();
}
}
3. Press Shift+F9 to debug the application on the Android emulator. You will see the progress
dialog for three (3) seconds.
How it Works
To create a progress dialog, you create an instance of the ProgressDialog class and call its show()
method:
progressDialog = ProgressDialog.show (this.”Please wait”,”Processing…”,true)
• Because this is a model dialog, it will block the UI until it is dismissed. To close the dialog, you
create a timer that calls the dismiss() method after three seconds.
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" >
<intent-filter >
<action android:name=“….usingintent.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
5
4. Make a copy of the activity_main.xml file (in the res/layout folder) by right-clicking it and
selecting Copy. Then right-click the res/layout folder and select Paste. Name the file
activity_second.xml.
5. Modify the activity_second.xml as follows:
6. In the SecondActivity.java file, add the bolded statements from the following code:
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
7. Add the bolded lines in the following code to the activity_main.xml files:
6
7. Modify the MainActivity.java file as shown in the bolded lines in the following code:
8. Press Shift+F9 to debug the application on the Android emulator. When the first activity is
loaded, click the button and the second activity also loads.
How it Works
When you add a new activity to the application, be sure to note the following:
7
Returning Results from an Intent
The startActivity() method invokes another activity but does not return a result to the
current activity. For example, you might have an activity that enters a username and a password. The
information entered by the user in that activity needs to be passed back to the calling activity for further
processing. If you need to pass data back from an activity, you should instead use the
startActivityForResult( ) method.
1. Using the last project, modify the secondActivity.xml file to look like the following code.
<TextView
android:layout_width="wrap_content"
android: layout_height=" wrap_content "
android:text="This is the Second Activity"
android:id="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android: layout_height=" wrap_content "
android:text="Please enter your name"
android:id="@+id/textView3" />
<EditText
android:layout_width="match_parent"
android: layout_height=" wrap_content "
android:id="@+id/txtUsername" />
<Button
android:layout_width=" wrap_content "
android:layout_height=" wrap_content "
android:text="OK"
android:onClick = “onClick”
android:id="@+id/button2" />
</LinearLayout>
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
8
public class SecondActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
3. Add the bolded statements in the following code to the MainActivity.java file:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
4. Press Shift+F9 to debug the application on the Android Emulator. When the first activity is
loaded, click the button to load SecondActivity. Enter your name and click the OK button. The
first activity displays the name you have entered using the Toast class.
Besides returning data from an activity, it is also common to pass data to an activity. For
example, in the previous example, you might want to set some default text in the EditText view before
the activity is displayed. In this case, you can use the Intent object to pass the data to the target activity.
9
Passing Data to the Target Activity
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to go to Second Activity"
android:onClick="onClick"
android:id="@+id/button"
</LinearLayout>
3. Add a new XML file to the res/layout folder and name it activity_second.xml. Populate it as
follows:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the Second Activity!"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to go to Main Activity"
android:onClick="onClick"
android:id="@+id/button2" />
</LinearLayout>
4. Add a new Class file to the package and name it SecondActivity. Populate the
SecondActivity.java file as follows:
10
package com….
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
5. Add the bolded statements from the following code to the AndroidManifest.xml file:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
11
6. Add the bolded statements from the following code to the MainActivity.java file:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
7. Press Shift+F9 to debug the application in the Android emulator. Click the button on each
activity and observe the value displayed.
12