欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Activity與Intent詳解及示例代碼

 更新時(shí)間:2016年08月11日 15:22:03   投稿:lqh  
本文主要講解Android Activity與Intent的知識,這里整理了相關(guān)資料并附有示例代碼,有興趣的小伙伴可以參考下

Android有三個(gè)基礎(chǔ)組件Activity,ServiceBroadcastReceiver,他們都是依賴Intent來啟動(dòng)。本文介紹的是Activity的生命周期以及針對Activity的Intent使用。

       之前的例子一直都是使用Activity,在一個(gè)Layout XML與一個(gè)Activity捆綁的情況下可以視為一個(gè)Form,多個(gè)Layout XML與一個(gè)Activity捆綁的話那就是個(gè)Application本身了。Intent可以分為顯式Intent和隱式Intent:顯式Intent用于啟動(dòng)明確的目標(biāo)組件(前面所說的三大組件),同一個(gè)Application內(nèi)的多個(gè)Activity調(diào)用也是顯式Intent;隱式Intent就是調(diào)用沒有明確的目標(biāo)組件,可以是系統(tǒng)也可以是第三方程序。隱式Intent一般用于調(diào)用系統(tǒng)組件功能,相關(guān)例程都是網(wǎng)絡(luò)上很容易找到的(調(diào)用某些系統(tǒng)組件的時(shí)候要申請權(quán)限)。

       Acitivity的運(yùn)行狀況分為:onCreate、onDestroy、onStart、onStop、onRestart、onResume、onPause,onCreate對應(yīng)onDestroy,onStart對應(yīng)onStop,onResume對應(yīng)onPause。

       先貼出本文運(yùn)行截圖:

       這個(gè)是從Acitivity1轉(zhuǎn)到Activity2的時(shí)候,Acitivity1的狀態(tài)變化,使用了finish()會(huì)觸發(fā)onDestroy()。

       這個(gè)是從Activity2轉(zhuǎn)到Activity1的時(shí)候,Acitivity2的狀態(tài)變化。從兩次Activity的啟動(dòng)可以看出,流程是onCreate()->onStart()->onResume()三個(gè)方法,銷毀是onPause()->onStop()->onDestroy()。另外,要往工程添加第二個(gè)Activity,需要到AndroidManifest.xml->Application那里添加Activity2。

  main1.xml的源碼:

XML/HTML代碼

<?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="跳轉(zhuǎn)到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="跳轉(zhuǎn)到外部Activity"></Button> 
</LinearLayout> 

main2.xml的源碼:

XML/HTML代碼

<?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="返回Activity1"></Button> 
</LinearLayout> 

   Activity1的源碼:

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;  
public class testActivityIntent extends Activity {  
  /** Called when the activity is first created. */ 
  Button btnToInternalActivity;  
  Button btnToExternalActivity;  
  EditText tbBundle;  
  @Override 
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    Log.e("Activity1", "onCreate");//顯示當(dāng)前狀態(tài),onCreate與onDestroy對應(yīng)  
    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");//顯示當(dāng)前狀態(tài),onCreate與onDestroy對應(yīng)  
  }  
  @Override 
  public void onStart()  
  {  
    super.onStart();  
    Log.e("Activity1", "onStart");//顯示當(dāng)前狀態(tài),onStart與onStop對應(yīng)  
  }  
  @Override 
  public void onStop()  
  {  
    super.onStop();  
    Log.e("Activity1", "onStop");//顯示當(dāng)前狀態(tài),onStart與onStop對應(yīng)   
  }  
  @Override 
  public void onRestart()  
  {  
    super.onRestart();  
    Log.e("Activity1", "onRestart");  
  }  
  @Override 
  public void onResume()  
  {  
    super.onResume();  
    Log.e("Activity1", "onResume");//顯示當(dāng)前狀態(tài),onPause與onResume對應(yīng)   
    SharedPreferences prefs = getPreferences(0); //SharedPreferences 用于存儲(chǔ)數(shù)據(jù)  
    String restoredText = prefs.getString("editText01", null);  
    if (restoredText != null) {  
      this.tbBundle.setText(restoredText);  
    }  
  }  
  @Override 
  public void onPause()  
  {  
    super.onResume();  
    Log.e("Activity1", "onPause");//顯示當(dāng)前狀態(tài),onPause與onResume對應(yīng)   
    //保存文本框的內(nèi)容,使得重回本Acitivity的時(shí)候可以恢復(fù)  
    SharedPreferences.Editor editor = getPreferences(0).edit();//SharedPreferences 用于存儲(chǔ)數(shù)據(jù)  
    editor.putString("editText01", this.tbBundle.getText().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一個(gè)Bundle對象,并將要傳遞的數(shù)據(jù)傳入  
        Bundle bundle = new Bundle();  
        bundle.putString("Text",tbBundle.getText().toString());  
         
        //將Bundle對象assign給Intent  
        intent.putExtras(bundle);  
         
        //調(diào)用Activity2  
        startActivity(intent);  
          
        testActivityIntent.this.finish();//會(huì)觸發(fā)onDestroy();  
      }  
      else if(v==btnToExternalActivity)  
      {  
        //有些外部調(diào)用需要開啟權(quán)限  
        Uri uri = Uri.parse("http://google.com");   
        Intent it = new Intent(Intent.ACTION_VIEW, uri);   
        startActivity(it);  
      }  
        
    }  
      
  }  
    
} 

   Activity2的源碼:

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");//顯示當(dāng)前狀態(tài),onCreate與onDestroy對應(yīng)  
      
    //加載activity2.xml  
    setContentView(R.layout.main2);  
      
    //得Intent中的Bundle對象  
    Bundle bunde = this.getIntent().getExtras();  
    //取得Bundle對象中的數(shù)據(jù)  
    Log.i("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");//顯示當(dāng)前狀態(tài),onCreate與onDestroy對應(yīng)  
  }  
  @Override 
  public void onStart()  
  {  
    super.onStart();  
    Log.e("Activity2", "onStart");//顯示當(dāng)前狀態(tài),onStart與onStop對應(yīng)  
  }  
  @Override 
  public void onStop()  
  {  
    super.onStop();  
    Log.e("Activity2", "onStop");//顯示當(dāng)前狀態(tài),onStart與onStop對應(yīng)   
  }  
  @Override 
  public void onRestart()  
  {  
    super.onRestart();  
    Log.e("Activity2", "onRestart");    
  }  
  @Override 
  public void onResume()  
  {  
    super.onResume();  
    Log.e("Activity2", "onResume");//顯示當(dāng)前狀態(tài),onPause與onResume對應(yīng)     
  }  
  @Override 
  public void onPause()  
  {  
    super.onResume();  
    Log.e("Activity2", "onPause");//顯示當(dāng)前狀態(tài),onPause與onResume對應(yīng)   
  }  
    
  class ClickEvent implements View.OnClickListener{  
    @Override 
    public void onClick(View v) {  
      if(v==btnBackMain1)  
      {  
          
        Intent intent = new Intent();  
        intent.setClass(Activity2.this,testActivityIntent.class);  
          
        //調(diào)用Activity1  
        startActivity(intent);  
          
        Activity2.this.finish();//會(huì)觸發(fā)onDestroy();  
      }  
        
    }  
      
  }  
} 

以上就是Android Activity與Intent 的資料整理,后續(xù)繼續(xù)補(bǔ)充,謝謝大家對本站的支持!

相關(guān)文章

  • Android進(jìn)程間使用Intent進(jìn)行通信

    Android進(jìn)程間使用Intent進(jìn)行通信

    Android進(jìn)程間通信(IPC,Inter-Process Communication)底層采用的是 Binder 機(jī)制,具體到應(yīng)用層有網(wǎng)友根據(jù)安卓四大組件將進(jìn)程間通信方式分為對應(yīng)的四種方式 Activity, Broadcast, ContentProvider, Service
    2023-02-02
  • Flutter路由的跳轉(zhuǎn)、動(dòng)畫和傳參詳解(最簡單)

    Flutter路由的跳轉(zhuǎn)、動(dòng)畫和傳參詳解(最簡單)

    這篇文章主要給大家介紹了關(guān)于Flutter路由的跳轉(zhuǎn)、動(dòng)畫和傳參的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • Android 使用 DowanloadManager 實(shí)現(xiàn)下載并獲取下載進(jìn)度實(shí)例代碼

    Android 使用 DowanloadManager 實(shí)現(xiàn)下載并獲取下載進(jìn)度實(shí)例代碼

    這篇文章主要介紹了Android 使用 DowanloadManager 實(shí)現(xiàn)下載并獲取下載進(jìn)度實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Flutter?Widget之FutureBuilder使用示例詳解

    Flutter?Widget之FutureBuilder使用示例詳解

    這篇文章主要為大家介紹了Flutter?Widget之FutureBuilder使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android 帶箭頭的指引tipLayout實(shí)現(xiàn)示例代碼

    Android 帶箭頭的指引tipLayout實(shí)現(xiàn)示例代碼

    本篇文章主要介紹了Android 帶箭頭的指引tipLayout實(shí)現(xiàn)示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • 關(guān)于Android冷啟動(dòng)耗時(shí)優(yōu)化詳解

    關(guān)于Android冷啟動(dòng)耗時(shí)優(yōu)化詳解

    大家好,本篇文章主要講的是關(guān)于Android冷啟動(dòng)耗時(shí)優(yōu)化詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Android中aapt命令實(shí)踐

    Android中aapt命令實(shí)踐

    本篇文章給大家詳解了Android aapt命令介紹及常用命令實(shí)踐,有這方面需求的朋友跟著學(xué)習(xí)下吧。
    2018-01-01
  • AndroidStudio項(xiàng)目打包成jar的簡單方法

    AndroidStudio項(xiàng)目打包成jar的簡單方法

    JAR(Java Archive,Java 歸檔文件)是與平臺(tái)無關(guān)的文件格式,它允許將許多文件組合成一個(gè)壓縮文件,在eclipse中我們知道如何將一個(gè)項(xiàng)目導(dǎo)出為jar包,供其它項(xiàng)目使用呢?下面通過本文給大家介紹ndroidStudio項(xiàng)目打包成jar的簡單方法,需要的朋友參考下吧
    2017-11-11
  • Android如何給Textview添加菜單項(xiàng)詳解(Java)

    Android如何給Textview添加菜單項(xiàng)詳解(Java)

    TextView是android里面用的最多的控件,TextView類似一般UI中的Label,TextBlock等控件,只是為了單純的顯示一行或多行文本,下面這篇文章主要給大家介紹了關(guān)于Android如何給Textview添加菜單項(xiàng)的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Android編程實(shí)現(xiàn)的EditText彈出打開和關(guān)閉工具類

    Android編程實(shí)現(xiàn)的EditText彈出打開和關(guān)閉工具類

    這篇文章主要介紹了Android編程實(shí)現(xiàn)的EditText彈出打開和關(guān)閉工具類,涉及Android輸入框EditText彈出打開和關(guān)閉功能簡單實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-02-02

最新評論