Android入门第43天-Activity与Activity间的互相传值
Posted TGITCIC
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android入门第43天-Activity与Activity间的互相传值相关的知识,希望对你有一定的参考价值。
介绍
今天的课程会比较好玩,我们在之前的Service篇章中看到了一种putExtras和getExtras来进行activity与service间的传值。而恰恰这种传值其实也是android里的通用传值法。它同样可以适用在activity与activity间传值。
Android中的传值
传单个值
传多个值
具体我们来结合例子来看吧
课程目标
正向传值到下一个activity上
反向传值到调用activity上
- 正向传值用:Intent+Bundle传值;
- 反向传值我们使用:registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> 回调,并判断这个result.getResultCode();
- 同时,我们制作了一个ActivityCollector extends Application来注册所有打开的activity,然后通过这个控制类来实现一键关闭所有打开的activity;
来看代码实现吧
全代码
前端
表格控制中用到的item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:id="@+id/iconImg"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_centerInParent="true"
android:src="@drawable/icon_1_128"
/>
<TextView
android:id="@+id/iconText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iconImg"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="text"
android:textSize="18sp"
/>
</RelativeLayout>
表格控制用到的GenericAdapter(这个在我们之前讲Adapter的篇章中已经写过)
package org.mk.android.demo;
import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public abstract class
GenericAdapter <T> extends BaseAdapter
private List<T> data;
private int layoutRes;
public GenericAdapter()
public GenericAdapter(List<T> data, int layoutRes)
this.data = data;
this.layoutRes = layoutRes;
@Override
public int getCount()
Log.i("app",">>>>>>data.size: "+data.size());
if(data!=null)
return data.size();
return 0;
@Override
public T getItem(int position)
return data.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
Log.i("app",">>>>>>into getView");
ViewHolder holder = ViewHolder.bind(parent.getContext(), convertView, parent, layoutRes
, position);
bindView(holder, getItem(position));
return holder.getItemView();
//添加一个元素
public void add(T item)
if (data == null)
data = new ArrayList<>();
data.add(item);
notifyDataSetChanged();
//往特定位置,添加一个元素
public void add(int position,T item)
if (data == null)
data = new ArrayList<>();
data.add(position, item);
notifyDataSetChanged();
public void remove(T item)
if(data != null)
data.remove(item);
notifyDataSetChanged();
public void remove(int position)
if(data != null)
data.remove(position);
notifyDataSetChanged();
public void clear()
if(data != null)
data.clear();
notifyDataSetChanged();
public abstract void bindView(ViewHolder holder, T obj);
public static class ViewHolder
private SparseArray<View> mViews; //存储ListView 的 item中的View
private View item; //存放convertView
private int position; //游标
private Context context; //Context上下文
//构造方法,完成相关初始化
private ViewHolder(Context context, ViewGroup parent, int layoutRes)
mViews = new SparseArray<>();
this.context = context;
View convertView = LayoutInflater.from(context).inflate(layoutRes, parent,false);
convertView.setTag(this);
item = convertView;
public static ViewHolder bind(Context context, View convertView, ViewGroup parent,
int layoutRes, int position)
ViewHolder holder;
if(convertView == null)
holder = new ViewHolder(context, parent, layoutRes);
else
holder = (ViewHolder) convertView.getTag();
holder.item = convertView;
holder.position = position;
return holder;
public <T extends View> T getView(int id)
T t = (T) mViews.get(id);
if(t == null)
t = (T) item.findViewById(id);
mViews.put(id, t);
return t;
/**
* 获取当前条目
*/
public View getItemView()
return item;
/**
* 获取条目位置
*/
public int getItemPosition()
return position;
/**
* 设置文字
*/
public ViewHolder setText(int id, CharSequence text)
View view = getView(id);
if(view instanceof TextView)
((TextView) view).setText(text);
return this;
/**
* 设置图片
*/
public ViewHolder setImageResource(int id, int drawableRes)
View view = getView(id);
if(view instanceof ImageView)
((ImageView) view).setImageResource(drawableRes);
else
view.setBackgroundResource(drawableRes);
return this;
/**
* 设置标签
*/
public ViewHolder setTag(int id, Object obj)
getView(id).setTag(obj);
return this;
public ImageView iconImg;
public TextView iconText;
activity_home_page.xml文件
- activity_main.xml文件里点击【注册】按钮跳到activity_home_page.xml文件。
- 然后把activity_main.xml文件中的内容显示在activity_home_page.xml的toast中。
- 在activity_home_page.xml文件中选择一个图标后返回activity_main.xml,并显示用户选择的内容;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context=".HomePageActivity">
<!--numColumns设置每行显示多少个-->
<GridView
android:id="@+id/gridPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" />
</RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入注册信息:"
android:textSize="18dp" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="18dp" />
<EditText
android:id="@+id/edTextLoginName"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="text"
android:text="" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="性别:"
android:textSize="18dp" />
<RadioGroup
android:id="@+id/rgGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioMan"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="30dip"
android:text="男性" />
<RadioButton
android:id="@+id/radioWoman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女性" />
</RadioGroup>
</TableRow>
<TableRow>
<Button
android:id="@+id/buttonRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_span="2"
android:text="注册"
android:textSize="18dp" />
</TableRow>
<TableRow>
<ImageView
android:id="@+id/selectedImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2" />
</TableRow>
</TableLayout>
后端代码
ActivityCollector
用于收集和一键关闭所有打开的Activity用
package org.mk.android.demo;
import android.app.Activity;
import android.app.Application;
import android.util.Log;
import java.util.LinkedList;
import java.util.List;
public class ActivityCollector extends Application
//运用list来保存们每一个activity是关键
private List<Activity> mList = new LinkedList<Activity>();
//为了实现每次使用该类时不创建新的对象而创建的静态对象
private static ActivityCollector instance;
//构造方法
private ActivityCollector()
//实例化一次
public synchronized static ActivityCollector getInstance()
if (null == instance)
instance = new ActivityCollector();
return instance;
//addActivity
public void addActivity(Activity activity)
mList.add(activity);
//removeOneActivity
public void removeActivity(Activity activity)
mList.remove(activity);
//关闭每一个list内的activity
public void exit()
try
if (mList != null && mList.size() > 0)
Log.i("app", ">>>>>>activity size->" + mList.size());
for (Activity activity : mList)
if (activity != null)
Log.i("app", ">>>>>>remove activity...");
activity.finish();
else
Log.i("app", ">>>>>>there is no activity need to be closed!");
catch (Exception e)
e.printStackTrace();
finally
System.exit(0);
//杀进程
public void onLowMemory()
super.onLowMemory();
System.gc();
IconBean.java
package org.mk.android.demo;
import java.io.Serializable;
public class IconBean implements Serializable
public IconBean(int imgId, String iconText)
this.imgId = imgId;
this.iconText = iconText;
private int imgId;
private String iconText = "";
public int getImgId()
return imgId;
public void setImgId(int imgId)
this.imgId = imgId;
public String getIconText()
return iconText;
public void setIconText(String iconText)
this.iconText = iconText;
HomePageActivity.java
package org.mk.android.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class HomePageActivity extends AppCompatActivity
private String loginName;
private int gender;
private Context ctx;
private GridView gridPhoto;
private BaseAdapter adapter = null;
private List<IconBean> data = null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
ActivityCollector.getInstance().addActivity(this);
Intent it = getIntent();
Bundle bd = it.getExtras();
loginName = bd.getString("loginName");
gender = bd.getInt("gender");
Toast.makeText(HomePageActivity.this, "当前输入内容,性别:" + gender + " 登录名:" + loginName, Toast.LENGTH_LONG).show();
gridPhoto = (GridView) findViewById(R.id.gridPhoto);
data = new ArrayList<IconBean>();
data.add(new IconBean(R.drawable.icon_1_128, "星爸爸"));
data.add(new IconBean(R.drawable.icon_2_128, "金拱门"));
data.add(new IconBean(R.drawable.icon_3_128, "机器人"));
data.add(new IconBean(R.drawable.icon_4_128, "小企鹅"));
data.add(new IconBean(R.drawable.icon_5_128, "凯瑞肯"));
data.add(new IconBean(R.drawable.icon_6_128, "小肥皂"));
data.add(new IconBean(R.drawable.icon_7_128, "流浪者"));
adapter = new GenericAdapter<IconBean>(data, R.layout.item_list)
@Override
public void bindView(ViewHolder holder, IconBean obj)
holder.setImageResource(R.id.iconImg, obj.getImgId());
holder.setText(R.id.iconText, obj.getIconText());
;
gridPhoto.setAdapter(adapter);
gridPhoto.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
Intent it = getIntent();
Bundle bd = new Bundle();
bd.putInt("selectedId",data.get(position).getImgId());
bd.putString("selectedText",data.get(position).getIconText());
it.putExtras(bd);
setResult(101,it);
finish();
);
@Override
protected void onDestroy()
super.onDestroy();
ActivityCollector.getInstance().removeActivity(this);
MainActivity.java
package org.mk.android.demo;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.LinkedList;
public class MainActivity extends AppCompatActivity
private Button buttonRegister;
private RadioGroup genderRG;
private EditText edTextLoginName;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCollector.getInstance().addActivity(this);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
genderRG = (RadioGroup) findViewById(R.id.rgGroup);
edTextLoginName = (EditText) findViewById(R.id.edTextLoginName);
buttonRegister.setOnClickListener(new OnClickListener());
@Override
protected void onDestroy()
super.onDestroy();
ActivityCollector.getInstance().exit();
//下面这段是配合着老式的startActivityForResult(homeIt,101);的写法,当我们用了registerForActivityResult
//下面这种onActivityResult就不需要写了。
//@Override
//protected void onActivityResult(int requestCode, int resultCode,
// Intent data)
// super.onActivityResult(requestCode, resultCode, data);
// if (requestCode == 101 && resultCode == 101)
// Bundle bd = data.getExtras();
// int imgid = bd.getInt("selectedId");
// String selectedContent = bd.getString("selectedText");
// ImageView img = (ImageView) findViewById(R.id.selectedImg);
// img.setImageResource(imgid);
// Toast.makeText(MainActivity.this, "你刚才选择的是:" + selectedContent,
// Toast.LENGTH_LONG).show();
//
//
private ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result ->
if (result.getResultCode() == 101)
Bundle bd = result.getData().getExtras();
int imgid = bd.getInt("selectedId");
String selectedContent = bd.getString("selectedText");
ImageView img = (ImageView) findViewById(R.id.selectedImg);
img.setImageResource(imgid);
Toast.makeText(MainActivity.this, "你刚才选择的是:" + selectedContent, Toast.LENGTH_LONG).show();
);
private class OnClickListener implements View.OnClickListener
private int gender = -1;
private String loginName = "";
@Override
public void onClick(View v)
for (int i = 0; i < genderRG.getChildCount(); i++)
RadioButton rb = (RadioButton) genderRG.getChildAt(i);
if (rb.isChecked())
switch (i)
case 0:
gender = 0;
break;
case 1:
gender = 1;
break;
break;
loginName = edTextLoginName.getText().toString();
//Toast.makeText(MainActivity.this, "当前输入内容,性别:" + gender + "
// 登录名: " + loginName, Toast.LENGTH_SHORT).show();
Log.i("app", "当前输入内容,性别:" + gender + " 登录名:" + loginName);
transferToHomePage(loginName, gender);
private void transferToHomePage(String loginName, int gender)
Intent homeIt = new Intent(MainActivity.this, HomePageActivity.class);
Bundle bd = new Bundle();
bd.putString("loginName", loginName);
bd.putInt("gender", gender);
homeIt.putExtras(bd);
//startActivity(homeIt);
//startActivityForResult(homeIt,101);
// 这种写法已经废弃用新的registerForActivityResult会非常优雅且不用去覆盖onActivityResult
launcher.launch(homeIt);
传递值到子activity-HomePageActivity里使用putExtras自然没得话说。关键在于我们运行sub activity的launcher.launch(homeIt);方法。这个方法通过一个registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result ->来判断当子activity返回即调用了finish()生命周期后,可以得到子activity在finish()语句前setResult(101,it);中的内容即实现了子activity中的值回传上一层activity了。
自己请动一下手试试看吧。
附、工程结构
以上是关于Android入门第43天-Activity与Activity间的互相传值的主要内容,如果未能解决你的问题,请参考以下文章
Android入门第60天-MVVM中的Databinding与ListView结合使用
Android入门第41天-Android中的Service(bindService)