Activity lifecycle event : Activity « UI « Android






Activity lifecycle event

     

package app.test;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Test extends Activity {
  String tag = "Events";
  int request_Code = 1;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // hides the title bar
    // requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);
    Log.d(tag, "In the onCreate() event");
  }

  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
      // startActivity(new Intent("net.learn2develop.ACTIVITY2"));
      // startActivity(new Intent(this, Activity2.class));
      /*
       * startActivityForResult(new Intent(
       * "net.learn2develop.ACTIVITY2"), request_Code);
       */
      Intent i = new Intent("app.test.ACTIVITY2");

      Bundle extras = new Bundle();
      extras.putString("Name", "Your name here");
      i.putExtras(extras);
      startActivityForResult(i, 1);
    }
    return false;
  }

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == request_Code) {
      if (resultCode == RESULT_OK) {
        Toast.makeText(this, data.getData().toString(),
            Toast.LENGTH_SHORT).show();
      }
    }
  }

  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");
  }
}

class Activity2 extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.row);

    String defaultName = "";
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      defaultName = extras.getString("Name");
    }
    EditText txt_username = (EditText) findViewById(R.id.txt_username);
    txt_username.setHint(defaultName);
    Button btn = (Button) findViewById(R.id.btn_OK);
    btn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        Intent data = new Intent();
        EditText txt_username = (EditText) findViewById(R.id.txt_username);
        data.setData(Uri.parse(txt_username.getText().toString()));
        setResult(RESULT_OK, data);
        finish();
      }
    });
  }
}
class Activity3 extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.row);
    Button btn = (Button) findViewById(R.id.btn_OK);
    btn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        Intent data = new Intent();
        EditText txt_username = (EditText) findViewById(R.id.txt_username);
        data.setData(Uri.parse(txt_username.getText().toString()));
        setResult(RESULT_OK, data);
        finish();
      }
    });
  }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>
//row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Please enter your name" />    
<EditText
    android:id="@+id/txt_username"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" />    
<Button
    android:id="@+id/btn_OK"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="OK" />
</LinearLayout>

   
    
    
    
    
  








Related examples in the same category

1.Backup Activity
2.Notification Activity
3.Timing Activity
4.Set content view from xml for Activity
5.More than one Activity
6.Find user control by using findViewById
7.A Simple Form
8.Link form with POJO
9.Life cycle
10.Comparing Android UI Elements to Swing UI Elements
11.add android:background = "#FFFF0000"
12.Rotation demo
13.Set activity screen Orientation
14.Check activity result
15.Activity key event
16.Allows the activity to manage the Cursor's lifecyle based on the activity’s lifecycle---
17.Activity configuration changed event
18.Check Activity result and onActivityResult
19.Phone Call Activity
20.Using Media Store Activity
21.External Storage Activity
22.Making Activity Go Full-Screen
23.Surface View Test Activity
24.Widget Preview Activity
25.This class provides a basic demonstration of how to write an Android activity.
26.This demonstrates the basic code needed to write a Screen activity
27.Example of removing yourself from the history stack after forwarding to another activity.
28.Fancy Blur Activity
29.Example of receiving a result from another activity.
30.Demonstrates required behavior of saving and restoring dynamic activity state
31.Securer Activity
32.This activity is an example of a simple settings screen that has default values.
33.Bind Click Action Activity
34.get ActivityManager
35.forward to another Activity
36.Request window features before setContentView
37.No code required here to attach the listener
38.Save data to Preference
39.Save instance state
40.onRetainNonConfigurationInstance
41.Create a user interface in code.
42.Redirect
43.Demonstrates how the various soft input modes impact window resizing.
44.extends Activity implements OnClickListener