Android Improved Activity+intent usage example _android

Source: Internet
Author: User
Tags gettext

Generally speaking. People familiar with Android programming know that Android has three basic components Activity,service and broadcastreceiver, all of which rely on intent to start. This article describes the lifecycle of activity and the use of intent for 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 this article to run the screenshot as follows:


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, 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 source code is as follows:

<?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>
</LinearLayout>

Main2.xml source code is as follows:

<?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>
</LinearLayout>

Activity1 Java source code is as follows:

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 '
 alactivity;
 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"),//Displays the current status, OnCreate and OnDestroy corresponds} @Override public void OnStart () {Super.onstart
   ();
   LOG.E ("Activity1", "OnStart"),//Displays the current status, OnStart and OnStop corresponds} @Override public void OnStop () {super.onstop (); LOG.E ("Activity1", "OnStop"),//Displays the current state, OnStart and OnStop corresponds} @Override public void Onrestart () {Super.onrestart (
   );
  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 it is back to this AcitiVity can be restored sharedpreferences.editor Editor = getpreferences (0). Edit ();//sharedpreferences is used to store data editor.putstring (
    "EditText01", This.tbBundle.getText (). toString ());
  Editor.commit (); Class Clickevent implements view.onclicklistener{@Override public void OnClick (View v) {if (V==btntointernalactiv
       ity) {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.com");  
  Intent it = new Intent (Intent.action_view, URI); 
  StartActivity (IT);

 }
 }
  }
}

The Java source code for the

Activity2 is as follows:

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 current status, OnCreate and OnDestroy corresponding//Loading 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"),//Displays the current status, OnCreate and OnDestroy corresponds} @Override public void OnStart () {Super.onstart
   (); LOG.E ("Activity2", "OnStart"),//display current status, OnStart and OnStop corresponding} @Override public void OnStop () {super.onstop (); LOG.E ("Activity2", "OnStop"),//display current status, OnStart and onStop corresponding} @Override public void Onrestart () {Super.onrestart ()
   ; 
  LOG.E ("Activity2", "Onrestart");
   @Override public void Onresume () {super.onresume (); LOG.E ("Activity2", "Onresume"),//Displays the current state, OnPause and Onresume corresponds} @Override public void OnPause () {Super.onresume (
   ); LOG.E ("Activity2", "OnPause"),//display current status, OnPause and Onresume corresponding} class Clickevent implements view.onclicklistener{@Overr
       IDE public void OnClick (View v) {if (v==btnbackmain1) {Intent Intent = new Intent ();
       
       Intent.setclass (Activity2.this,testactivityintent.class);
       
       Call Activity1 startactivity (intent);
  Activity2.this.finish ()//Will trigger OnDestroy ();

 }
 }
  }
}

The

hope that the acitivity usage described in this example will help you to develop your Android program.

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.