ActivityGroup使用方法

这个例子是在 http://www.anddev.org上看到的,写在这里供大家看1mol.
1.主类ActGroups.java:
package com.ex;

import java.util.Stack;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ViewAnimator;

public class ActGroups extends ActivityGroup {

	/** 容纳单个Activity的容器. */
	private ViewAnimator mAnimator;
	/** 管理一组Activity的管理类. */
	public LocalActivityManager mActivityManager;
	/** 管理一组Activity对应标签的类. */
	private Stack<String> mIds;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findView();
		showNoticeActivity();
	}

	public void showNoticeActivity() {
		Intent mIntent = new Intent(this, NoticeActivity.class);
		launchActivity(mIntent, StringPool.CONST_NOTICE);
		((NoticeActivity) mActivityManager.getActivity(StringPool.CONST_NOTICE)).checkNotice();
	}

	public void findView() {
		mIds = new Stack<String>();
		mAnimator = (ViewAnimator) findViewById(R.id.animator);
		mActivityManager = getLocalActivityManager();
	}

	public void launchActivity(Intent intent, String mId) {
		int mIndex = -1;
		/** Check the activity existed or not. */
		for (int i = 0; i < mIds.size(); i++) {
			if (mIds.get(i).equals(mId)) {
				mIndex = i;
				break;
			}
		}
		if (mIndex == -1) {
			mIds.push(mId);
			for (int i = 0; i < mIds.size(); i++) {
				if (mIds.get(i).equals(mId)) {
					mIndex = i;
					break;
				}
			}
			View view = mActivityManager.startActivity(mId, intent).getDecorView();
			mAnimator.addView(view);
		}
		mAnimator.setDisplayedChild(mIndex);
	}
}


2.属于ActivityGroup的独立的类NoticeActivity.java:
package com.ex;

import android.app.Activity;
import android.os.Bundle;

public class NoticeActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.topic_info);
	}

	public void checkNotice() {
	}

}



3.另外还有一个标示各个Activity的常量类:
package com.ex;

public class StringPool {

	/** Show the add pictures page. */
	public static String CONST_NOTICE = "NoticeActivity";

}



4.main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ViewAnimator
        android:id="@+id/animator"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:inAnimation="@anim/fadein"
        android:outAnimation="@anim/fadeout" />

</RelativeLayout>


5.fadein.xml:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="1.0" />



6.fadeout.xml:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/decelerate_interpolator"
	android:zAdjustment="top" android:fromAlpha="1.0" android:toAlpha="0.0"
	android:duration="100" />



个人感觉比较好的几点:
1.管理各个Activity比较清晰,设置了Activity启动模式后,可以很好的对各个Activity进行管理和操作.

2.实现Activity之间切换动画效果.

你可能感兴趣的:(ActivityGroup)