关闭

android中activity管理及界面传值

标签: activity管理appandroid传值
189人阅读 评论(0) 收藏 举报
分类:

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




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10755次
    • 积分:329
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:0篇
    • 译文:0篇
    • 评论:3条