Android實(shí)現(xiàn)Activity之間通信的方法
本文實(shí)例講述了Android實(shí)現(xiàn)Activity之間通信的方法。分享給大家供大家參考。具體如下:
在一個Activity中可以使用系統(tǒng)提供的startActivity(Intent intent)方法打開新的Activity,在打開新的Activity前,你可以決定是否為新的Activity傳遞參數(shù)。
第一種:打開新的Activity,不傳遞參數(shù)
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnOpen=(Button)this.findViewById(R.id.btnOpen);
btnOpen.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//新建一個顯式意圖,第一個參數(shù)為當(dāng)前Activity類對象,第二個參數(shù)為你要打開的Activity類
startActivity(new Intent(MainActivity.this, OtherActivity.class));
}
});
}
}
第二種:打開新的Activity,并傳遞若干個參數(shù)給它
package com.ljq.activitys;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnOpen=(Button)this.findViewById(R.id.btnOpen);
btnOpen.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//第一種:打開新的Activity,不傳遞參數(shù)
//新建一個顯式意圖,第一個參數(shù)為當(dāng)前Activity類對象,第二個參數(shù)為你要打開的Activity類
//startActivity(new Intent(MainActivity.this, OtherActivity.class));
//第二種:打開新的Activity,并傳遞若干個參數(shù)給它
Intent intent=new Intent(MainActivity.this, OtherActivity.class);
//Bundle類用作攜帶數(shù)據(jù)
Bundle bundle=new Bundle();
bundle.putString("name", "linjiqin");
bundle.putInt("age", 24);
//附帶上額外的數(shù)據(jù)
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
在新的Activity中接收前面Activity傳遞過來的參數(shù)
package com.ljq.activitys;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class OtherActivity extends Activity {
private final static String TAG="OtherActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
//在新的Activity中接收前面Activity傳遞過來的參數(shù)
Bundle bundle=this.getIntent().getExtras();
String name=bundle.getString("name");
Integer age=bundle.getInt("age");
Log.i(TAG, name+" : "+age);
}
}
希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。
- Android的Activity加載方式實(shí)例分析
- Android編程之四種Activity加載模式分析
- Android編程四大組件之Activity用法實(shí)例分析
- 深入解讀Android開發(fā)中Activity的生命周期
- Android編程獲取并設(shè)置Activity亮度的方法
- Android編程中activity的完整生命周期實(shí)例詳解
- Android編程之Activity中onDestroy()調(diào)用分析
- 簡單介紹Android開發(fā)中的Activity控件的基本概念
- Android編程實(shí)現(xiàn)兩個Activity之間共享數(shù)據(jù)及互相訪問的方法
- Android Activity中onStart()和onResume()的區(qū)別分析
相關(guān)文章
Android通過自定義Activity實(shí)現(xiàn)懸浮的Dialog詳解
這篇文章主要給大家介紹了關(guān)于Android通過自定義Activity實(shí)現(xiàn)懸浮的Dialog的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對大家具有一定的參考學(xué)習(xí)價值,感興趣的朋友們下面來一起看看吧。2017-05-05
android針對json數(shù)據(jù)解析方法實(shí)例分析
這篇文章主要介紹了android針對json數(shù)據(jù)解析方法,以實(shí)例形式較為詳細(xì)的分析了Android操作json格式數(shù)據(jù)的各種常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android項(xiàng)目實(shí)現(xiàn)黑名單攔截效果
本篇文章主要介紹了Android項(xiàng)目實(shí)現(xiàn)黑名單攔截效果,可以根據(jù)白名單或者黑名單攔截,測試可以攔截電話,有需要的朋友可以了解一下。2016-10-10
Android自定義控件實(shí)現(xiàn)帶文字提示的SeekBar
這篇文章主要給大家介紹了關(guān)于Android自定義控件實(shí)現(xiàn)帶文字提示的SeekBar的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
Android編程中聊天頁面背景圖片、標(biāo)題欄由于鍵盤引起問題的解決方法
這篇文章主要介紹了Android編程中聊天頁面背景圖片、標(biāo)題欄由于鍵盤引起問題的解決方法,針對鍵盤彈出時標(biāo)題欄及背景圖片異常的相關(guān)解決方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android如何實(shí)現(xiàn)掃描和生成二維碼
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)掃描和生成二維碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
Android實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Android開發(fā)-之環(huán)境的搭建(圖文詳解)
這篇文章主要介紹了Android開發(fā)-之環(huán)境的搭建(圖文詳解),具有一定的參考價值,有興趣的可以了解一下。2016-11-11

