本文只要讲解:
(1)ActivityGroup中的activity切换动画
ActivityGroup中有一堆activity,我们可以很轻松的设置前进动画,和返回动画。
(2)ActivityGroup向任意一个子activity传送消息
比如,我们在一个子activity使用了startActivityForResult,那么返回时,调用的是activityGroup对象的onActivityResult方法,而不是这个子activity的onActivityResult方法,
所以我们就必须让activityGroup在onActivityResult中将消息传给那个子activity。
(1)切换动画
采用viewAnimator
1、设置进入和出来动画:
viewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_right));
viewAnimator.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_to_left));
2、显示前一个页面和后一个页面:
viewAnimator.showNext();
viewAnimator.showPrevious();
3、页面前进方法:
class1 : 前进要打开的activity类
bundle : 参数传递
activityString : 要打开的activity的字符标记,方便activityGroup定位
public void gotoActivity(Bundle bundle, Class<?> class1, String activityString) {
Intent intent = new Intent(this, class1).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtras(bundle);
Window w = getLocalActivityManager().startActivity(activityString, intent);
currentActivityString.add(activityString);
viewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_right));
viewAnimator.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_to_left));
View view = w.getDecorView();
history.add(view);
viewAnimator.addView(view);
viewAnimator.showNext();
group.setContentView(viewAnimator);
}
4、页面返回方法:
动画设置类似
public void onBackPressedAction() {
if (history.size() > 1) {
viewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_left));
viewAnimator.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_to_right));
viewAnimator.showPrevious();
viewAnimator.removeView(history.get(history.size() - 1));currentActivityString.remove(currentActivityString.size() - 1);
history.remove(history.size() - 1);
history.get(history.size() - 1).clearFocus();
group.setContentView(viewAnimator);
} else {
showDialog(DIALOG_APP_EXIT);
}
}
group : activitygroup对象
history : 作为一个页面浏览历史记录,以一个堆栈的形式维护。堆顶便是当前activity的view。
(2)传送消息
如果我们用
group.getLocalActivityManager().getCurrentActivity()
获取到的不是我们当前显示的页面,是group中最后打开的那个activity。
如果 :
我们从A -> B -> C -> D
在返回 -> C的时候
得到的是D,而不是C。怎么才能获取当前显示的activity呢,
我设置了一个堆栈变量
/*
* the getCurrentActivity method returns the last Activity
*
* this field is used for get currently displayed activity
*/
public List<String> currentActivityString = new ArrayList<String>();
在页面前进和后退中修改他的值,获取当前activity的方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
switch (resultCode) {
case RESULT_OK:
String string = currentActivityString.get(currentActivityString.size() - 1);
if (string == "Jingxuan")
((Jingxuan) getLocalActivityManager().getActivity("Jingxuan")).onActivityResult(requestCode,
resultCode, data);
break;
}
}
}
这样就在activityGroup对象的onActivityResult方法中将消息传给那个子activity。
MyActivityGroup.java 整个代码如下:
import java.util.ArrayList;
import java.util.List;
import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.ViewAnimator;
public class MyActivityGroup extends ActivityGroup {
public MyActivityGroup group = null;
/*
* the getCurrentActivity method returns the last Activity
*
* this field is used for get currently displayed activity
*/
public List<String> currentActivityString = new ArrayList<String>();
public ArrayList<View> history = null;
/**
* 页面切换的动画
*/
public ViewAnimator viewAnimator = null;
public final int DIALOG_APP_EXIT = 0;
public void onCreate(Bundle savedInstanceState) {
history = new ArrayList<View>();
viewAnimator = new ViewAnimator(this);
group = this;
super.onCreate(savedInstanceState);
}
public void gotoActivity(Bundle bundle, Class<?> class1, String activityString) {
Intent intent = new Intent(this, class1).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtras(bundle);
Window w = getLocalActivityManager().startActivity(activityString, intent);
currentActivityString.add(activityString);
viewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_right));
viewAnimator.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_to_left));
View view = w.getDecorView();
history.add(view);
viewAnimator.addView(view);
viewAnimator.showNext();
group.setContentView(viewAnimator);
}
/**
* 将事件传给子activity
*/
@Override
public void onBackPressed() {
getLocalActivityManager().getActivity(currentActivityString.get(currentActivityString.size() - 1))
.onBackPressed();
}
public void onBackPressedAction() {
if (history.size() > 1) {
viewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_left));
viewAnimator.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_to_right));
viewAnimator.showPrevious();
viewAnimator.removeView(history.get(history.size() - 1));
currentActivityString.remove(currentActivityString.size() - 1);
history.remove(history.size() - 1);
history.get(history.size() - 1).clearFocus();
group.setContentView(viewAnimator);
} else {
showDialog(DIALOG_APP_EXIT);
}
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case DIALOG_APP_EXIT:
dialog = new AlertDialog.Builder(this).setMessage("确定退出?").setTitle("提示")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
android.os.Process.killProcess(android.os.Process.myPid());
finish();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
}
return dialog;
}
}