android中activity管理及界面传值

android中利用application类中的ActivityLifecycleCallbacks接口实现activity的管理及界面传值


简化界面跳转,和界面之间的传值和获取

使用方法:

MainActivity中

AContext.push(SecondActivity.class, "SecondActivity");//跳转到SecondActivity中并传递一个字符串

String msg = AContext.getPushData();// SecondActivity中获取数据

ThreeActivity中:

AContext.pull("2", 3);//关闭当前activity并传递两个参数,

SecondActivity获取返回的数据

if (AContext.getPullData() != null) {

// 获取ThreeActivity返回的数据

String one = AContext.getPullData(0);

int two = AContext.getPullData(1);

AContext.show("第一页传过来的值:" +one + ";" + two);

}


/**

 * activity管理类

 *

 */

publicclass ActivityManagerimplements ActivityLifecycleCallbacks {


private Activity activity;


private ArrayList<Activity> activityList = new ArrayList<Activity>();


public ArrayList<Activity> getActivityList() {

returnactivityList;

}


public void exitLogin() {

Iterator<Activity> iterator =activityList.iterator();

while (iterator.hasNext() &&activityList.size() != 1) {

iterator.next().finish();

iterator.remove();

}

}


public void setActivity(Activityactivity) {

this.activity =activity;

}


public Activity getCurrentActivity() {

returnactivity;

}


/**

* 在activity创建时记录当前activity,并添加到集合中

*/

@Override

public void onActivityCreated(Activityactivity, Bundle savedInstanceState) {

Log.e("TAG","onActivityCreated");

setActivity(activity);

activityList.add(activity);

}


@Override

public void onActivityStarted(Activityactivity) {

Log.e("TAG","onActivityStarted");

}


/**

* 在resume中进行更新当前的activity

*/

@Override

public void onActivityResumed(Activityactivity) {

setActivity(activity);

Log.e("TAG","onActivityResumed");

}


@Override

public void onActivityPaused(Activityactivity) {

Log.e("TAG","onActivityPaused");

}


@Override

public void onActivityStopped(Activityactivity) {

Log.e("TAG","onActivityStopped");

}


@Override

public void onActivitySaveInstanceState(Activityactivity, Bundle outState) {

Log.e("TAG","onActivitySaveInstanceState");

}


/**

* 在activity销毁时移除

*/

@Override

public void onActivityDestroyed(Activityactivity) {

Log.e("TAG","onActivityDestroyed");

activityList.remove(activity);

}


private Object[] _pullData;


/**

* 获取回退前activity传递过来的参数

* @param position

* @return

*/

public <T extends Object> T getPullData(intposition) {

return isPullEmpty(position) ? (T)_pullData[position] : null;

}


private boolean isPullEmpty(intposition) {

return_pullData != null &&_pullData.length > position;

}


/**

* 关闭activity并回传

* @param data

*/

public void pullCurrentAcivity(Object...data) {

_pullData =data;

_pushData =null;

activity.finish();

}


/**

* finish

*/

public void pullActivity() {

pullCurrentAcivity();

}


/**

* push

*/

private Object[] _pushData;


/**

* 利用索引获取跳转前界面传递的数据

* @param position

* @return

*/

public <T extends Object> T getPushData(intposition) {

return isPushEmpty(position) ? (T)_pushData[position] : null;

}


private boolean isPushEmpty(intposition) {

return_pushData != null &&_pushData.length > position;

}


/**

* 跳转activity并传递参数

* @param activityClass

* @param pushData

*/

public void pushActivity(Class<?>activityClass, Object... pushData) {

_pushData =pushData;

_pullData =null;

Intent intent = new Intent(activity, activityClass);

activity.startActivity(intent);

}


          在AContext类中初始化ActivityManager并注册

        public staticvoid init(Application currentApplication) {

sApplication =currentApplication;

sActivityManager =new ActivityManager();

sApplication.registerActivityLifecycleCallbacks(sActivityManager);

}





通过application注册监听activity

public class InitApplicationextends Application {

@Override

public void onCreate() {

super.onCreate();

/**

* 初始化注册activity生命周期监听

*/

AContext.init(this);

}

}


最后在AndroidManifest.xml中注册application


csdn下载地址:http://download.csdn.net/detail/u010945409/9727455

github地址: https://github.com/xieyuhai/activity




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010945409/article/details/53993253
想对作者说点什么? 我来说一句

android内存管理

2011年07月22日 3KB 下载

Android 退出程序

2015年03月17日 2.04MB 下载

没有更多推荐了,返回首页

不良信息举报

android中activity管理及界面传值

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭