最近做的一个项目用到了ViewPager 加载四个Activity 实现。其中在做一个页面的TextView日期插入功能时受到了困扰。几经周折终于找到了解决的办法,现在和大家分享下。
在PlaneActivity 页面 使用 startActivityForResult()启动一个新的CalendarActivity。在该页面取值后调用setResult() 返回PlaneActivity。
这时候因为PlaneActivity 是在MainActivity的ViewPager 中使用 get_Main_View(String id, Intent intent) 调用的。该onActivityResult()事件会在MainActivity 中被拦截。所以我们需要借助一个借口来解决。
具体代码如下。
MyManagerApplication 代码
public class MyManagerApplication extends Application{
private static MyManagerApplication instance = null;
public static List<Activity> activityList = new LinkedList<Activity>();
/**
* @return
*/
private MyManagerApplication(){}
public static MyManagerApplication getInstance() {
// TODO Auto-generated method stub
if (null == instance) {
instance = new MyManagerApplication();
}
return instance;
}
public void addActivity(Activity activity) {
Log.i("TAG", "-------activityList" + activity.getClass().getName());
activityList.add(activity);
}
}
MainActivity 代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPager = (ViewPager) findViewById(R.id.main_lay);
manager = new LocalActivityManager(MainActivity.this, true);
manager.dispatchCreate(savedInstanceState);
}
… 此处需要Adapter 的配置
/**
* 启动对应的Activity
* */
private View get_Main_View(String id, Intent intent) {
@SuppressWarnings("deprecation")
Window window = manager.startActivity(id, intent);
return window.getDecorView();
}
//使用接口的回调把数据发还给子View,最重要的方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 获取当前活动的Activity实例
for (int i = 0; i < app.activityList.size(); i++) {
Activity subActivity = app.activityList.get(i);
// 判断是否实现返回值接口
if (
subActivity instanceof OnTabActivityResultListener) {
// 获取返回值接口实例
OnTabActivityResultListener listener = (OnTabActivityResultListener) subActivity;
// 转发请求到子Activity
listener.onTabActivityResult(requestCode, resultCode, data);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/*
* 接口类
*/
public interface OnTabActivityResultListener {
public void onTabActivityResult(int requestCode, int resultCode, Intent data);
}
PlaneActivity类 代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyManagerApplication app = MyManagerApplication.getInstance();
app.addActivity(PlaneActivity.this);
}
Void Show(){
Intent intent = new Intent(PlaneActivity.this,
CalendarView.class);
getParent().startActivityForResult(intent, REQUEST_CODE);
}
//接口的实现
@Override
public void onTabActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("plane-----ONResult--");
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Calendar cal = (Calendar) data.getSerializableExtra("getDate");
date.setText(CalendarUtil.getDay(cal));
}
super.onActivityResult(requestCode, resultCode, data);
}
CalendarActivity 类代码
/**
* 返回数据
*/
private void startForResult() {
// TODO Auto-generated method stub
intent = new Intent();
if (calSelected != null) {
intent.putExtra("getDate", calSelected);
} else {
intent.putExtra("getDate", calStartDate);
}
setResult(2, intent);
finish();
}