Android activity and intent detailed and sample code _android

Source: Internet
Author: User

Android has three basic components Activity,service and broadcastreceiver, and they all rely on intent to start. This article describes the life cycle of the activity and the use of intent for the activity.

The previous example has always been the use of activity, where a layout XML is tied to an activity as a form, and multiple layout XML tied to an activity is a application itself. Intent can be divided into explicit intent and implicit intent: Explicit intent is used to start a clear target component (the three major components mentioned earlier), and multiple activity calls within the same application are also explicit intent Implicit intent is the invocation of an ambiguous target component, either a system or a third-party program. Implicit intent are typically used to invoke system component functionality, which is easily found on the network (requesting permissions when invoking certain system components).

The operation status of Acitivity is divided into: onCreate, OnDestroy, OnStart, OnStop, Onrestart, Onresume, onpause,oncreate and corresponding OnDestroy, OnStart corresponds to onstop,onresume corresponding to OnPause.

First post the screenshot of this article:

This is when you go from Acitivity1 to Activity2, Acitivity1 state changes and using finish () triggers OnDestroy ().

This is the change from Activity2 to Activity1, Acitivity2 state. From the start of the two activity, it can be seen that the process is OnCreate ()->onstart ()->onresume () three methods, the destruction is OnPause ()->onstop ()->ondestroy (). In addition, to add a second activity to the project, you need to add Activity2 to androidmanifest.xml->application.

Main1.xml's source code:

xml/html Code

<?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 "> 
  <button android:layout_width=" wrap_content "android:layout_height=" Wrap_content " 
    Android:id= "@+id/main1. Button01 " 
    android:text=" jump to Activity2 "></Button> 
  <edittext android:text=" @+id/edittext01 " Android:id= "@+id/edittext01" 
    android:layout_width= "wrap_content" android:layout_height= "Wrap_content" > </EditText> 
  <button android:layout_width= "wrap_content" 
    android:layout_height= "Wrap_content" Android:id= "@+id/main1. Button02 " 
    android:text=" jump to external activity "></Button> 

Main2.xml's source code:

xml/html Code

<?xml version= "1.0" encoding= "UTF-8"?> <linearlayout android:id= 
"@+id/linearlayout01" 
  android: Layout_width= "Fill_parent" android:layout_height= "fill_parent" 
  xmlns:android= "http://schemas.android.com/" Apk/res/android "> 
  <button android:layout_width=" wrap_content " 
    android:layout_height=" wrap_content "Android:id=" @+id/main2. Button01 " 
    android:text=" back to Activity1 "></Button> 

Activity1 's source code:

Package com.testactivityintent;  
Import android.app.Activity;  
Import android.content.Intent;  
Import android.content.SharedPreferences;  
Import Android.net.Uri;  
Import Android.os.Bundle;  
Import Android.util.Log;  
Import Android.view.View;  
Import Android.widget.Button;  
Import Android.widget.EditText; The public class Testactivityintent extends activity {/** called the ' when the ' is the ' The activity ' is the ' the '  
  ternalactivity;  
  Button btntoexternalactivity;  
  EditText Tbbundle;  
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);  
      
    LOG.E ("Activity1", "onCreate"), and/or display the current status, OnCreate and OnDestroy correspond Setcontentview (r.layout.main1);  
    Btntointernalactivity= (Button) This.findviewbyid (R.ID.MAIN1_BUTTON01);  
    Btntoexternalactivity= (Button) This.findviewbyid (R.ID.MAIN1_BUTTON02);  
    Btntointernalactivity.setonclicklistener (New Clickevent ()); Btntoexternalactivity.setonclicklistener (nEW clickevent ());      
  Tbbundle= (EditText) This.findviewbyid (r.id.edittext01);  
    public void OnDestroy () {Super.ondestroy (); LOG.E ("Activity1", "OnDestroy"),//display current status, OnCreate and OnDestroy corresponding} @Override public void OnStart () {SUP  
    Er.onstart (); LOG.E ("Activity1", "OnStart"),//Displays the current status, OnStart and OnStop corresponds} @Override public void OnStop () {Super.onst  
    OP (); LOG.E ("Activity1", "OnStop"),//Displays the current status, OnStart and OnStop corresponds} @Override public void Onrestart () {SUPER.O  
    Nrestart ();  
  LOG.E ("Activity1", "Onrestart");  
    @Override public void Onresume () {super.onresume (); LOG.E ("Activity1", "Onresume"), and/or display the current status, OnPause and Onresume correspond sharedpreferences prefs = getpreferences (0);  
    Sharedpreferences is used to store data String Restoredtext = prefs.getstring ("editText01", null);  
    if (Restoredtext!= null) {This.tbBundle.setText (restoredtext); }} @Override PuBlic void OnPause () {super.onresume (); LOG.E ("Activity1", "OnPause"),//display the current status, OnPause and Onresume correspond//Save the contents of the text box so that you can restore acitivity when you return to this sharedpreferences. Editor Editor = getpreferences (0). Edit ();//sharedpreferences for storing Data editor.putstring ("editText01", This.tbBundle.get  
    Text (). toString ());  
  Editor.commit (); Class Clickevent implements view.onclicklistener{@Override public void OnClick (View v) {if  
      (v==btntointernalactivity)  
        {Intent Intent = new Intent ();  
          
        Intent.setclass (Testactivityintent.this,activity2.class);  
        New is a Bundle object, and the data to be passed is passed into the Bundle Bundle = new Bundle ();  
         
        Bundle.putstring ("Text", Tbbundle.gettext (). toString ());  
         
        Assign the bundle object to intent Intent.putextras (bundle);  
          
        Call Activity2 startactivity (intent);  
      TestActivityIntent.this.finish ()//Will trigger OnDestroy ();  
}      else if (v==btntoexternalactivity) {//Some external calls need to open the permission uri uri = Uri.parse ("http://google.co   
        M ");   
        Intent it = new Intent (Intent.action_view, URI);  
      StartActivity (IT);  }  
        
    }  
      
  }  
    
}

   Activity2 's source code:

Package com.testactivityintent;  
Import android.app.Activity;  
Import android.content.Intent;  
Import Android.os.Bundle;  
Import Android.util.Log;  
Import Android.view.View;  
Import Android.widget.Button;  
  public class Activity2 extends activity {Button btnBackMain1;  
    public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); LOG.E ("Activity2", "onCreate"),//display the current status, OnCreate and OnDestroy correspond//load Activity2.xml Setcontentview (r.layout.  
      
    MAIN2);  
    The Bundle object in the intent is Bundle Bunde = This.getintent (). Getextras ();  
    Obtains the data log.i in the Bundle object ("In_text", Bunde.getstring ("Text"));  
    btnbackmain1= (Button) This.findviewbyid (R.ID.MAIN2_BUTTON01);  
   Btnbackmain1.setonclicklistener (New Clickevent ());  
    public void OnDestroy () {Super.ondestroy (); LOG.E ("Activity2", "OnDestroy"),//display current status, OnCreate and OnDestroy corresponding} @Override public void OnStart () {SUP Er.OnStart (); LOG.E ("Activity2", "OnStart"),//Displays the current status, OnStart and OnStop corresponds} @Override public void OnStop () {Super.onst  
    OP (); LOG.E ("Activity2", "OnStop"),//Displays the current status, OnStart and OnStop corresponds} @Override public void Onrestart () {SUPER.O  
    Nrestart ();    
  LOG.E ("Activity2", "Onrestart");  
    @Override public void Onresume () {super.onresume (); LOG.E ("Activity2", "Onresume"),//display current status, OnPause and Onresume corresponding} @Override public void OnPause () {SUP  
    Er.onresume (); LOG.E ("Activity2", "OnPause"),//display current status, OnPause and Onresume corresponding} class Clickevent implements View.onclicklistener {@Override public void OnClick (View v) {if (v==btnbackmain1) {Intent int  
        ent = new Intent ();  
          
        Intent.setclass (Activity2.this,testactivityintent.class);  
          
        Call Activity1 startactivity (intent); Activity2.this.finish ()//Will touchHair OnDestroy ();  }  
        
    }  
      
  }  
}

Above is the Android activity and intent data collation, follow-up continue to add, thank you for your support to this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.