今天的课程会比较好玩,我们在之前的Service篇章中看到了一种putExtras和getExtras来进行activity与service间的传值。而恰恰这种传值其实也是Android里的通用传值法。它同样可以适用在activity与activity间传值。
具体我们来结合例子来看吧
来看代码实现吧
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 extends BaseAdapter {
private List data;
private int layoutRes;
public GenericAdapter() {
}
public GenericAdapter(List 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 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 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用
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 mList = new LinkedList();
//为了实现每次使用该类时不创建新的对象而创建的静态对象
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();
}
}
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;
}
}
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 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();
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(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);
}
}
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了。
自己请动一下手试试看吧。