Android之Activity框架

痛定思痛。 2022-07-27 15:54 268阅读 0赞

在安卓应用中,经常需要Activity中经常需要有大量相似的Activity类,这些类往往有相似的结构与功能,因此产生了大量重复代码,为此,以下提供一种方法有效的降低了代码冗余。

定义Activity工具类

  1. * 应用程序Activity管理类:用于Activity管理和应用程序退出
  2. * 修订历史
  3. *
  4. * ============================================================
  5. **/
  6. public class AppManager {
  7. private static Stack<Activity> activityStack;
  8. private static AppManager instance;
  9. private AppManager(){}
  10. /**
  11. * 单一实例
  12. */
  13. public static AppManager getAppManager(){
  14. if(instance==null){
  15. instance=new AppManager();
  16. }
  17. return instance;
  18. }
  19. /**
  20. * 添加Activity到堆栈
  21. */
  22. public void addActivity(Activity activity){
  23. if(activityStack==null){
  24. activityStack=new Stack<Activity>();
  25. }
  26. activityStack.add(activity);
  27. }
  28. /**
  29. * 获取当前Activity(堆栈中最后一个压入的)
  30. */
  31. public Activity currentActivity(){
  32. Activity activity=activityStack.lastElement();
  33. return activity;
  34. }
  35. /**
  36. * 结束当前Activity(堆栈中最后一个压入的)
  37. */
  38. public void finishActivity(){
  39. Activity activity=activityStack.lastElement();
  40. finishActivity(activity);
  41. }
  42. /**
  43. * 结束指定的Activity
  44. */
  45. public void finishActivity(Activity activity){
  46. if(activity!=null){
  47. activityStack.remove(activity);
  48. activity.finish();
  49. activity=null;
  50. }
  51. }
  52. /**
  53. * 结束指定类名的Activity
  54. */
  55. public void finishActivity(Class<?> cls){
  56. for (Activity activity : activityStack) {
  57. if(activity.getClass().equals(cls) ){
  58. finishActivity(activity);
  59. }
  60. }
  61. }
  62. /**
  63. * 结束所有Activity
  64. */
  65. public void finishAllActivity(){
  66. for (int i = 0, size = activityStack.size(); i < size; i++){
  67. if (null != activityStack.get(i)){
  68. activityStack.get(i).finish();
  69. }
  70. }
  71. activityStack.clear();
  72. }
  73. /**
  74. * 退出应用程序
  75. */
  76. public void AppExit(Context context) {
  77. try {
  78. finishAllActivity();
  79. ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  80. activityMgr.restartPackage(context.getPackageName());
  81. System.exit(0);
  82. android.os.Process.killProcess(android.os.Process.myPid());
  83. } catch (Exception e) { }
  84. }
  85. }

定义Activity基类

  1. public abstract class BaseActivity extends Activity implements OnClickListener {
  2. /**
  3. * Android生命周期回调方法-创建
  4. */
  5. @Override
  6. public void onCreate(Bundle paramBundle) {
  7. super.onCreate(paramBundle);
  8. // 设置没有标题
  9. // requestWindowFeature(Window.FEATURE_NO_TITLE);
  10. mContext = this;
  11. app = (AmbowApplication) getApplication();
  12. AppManager.getAppManager().addActivity(this);
  13. initView();
  14. }
  15. /**
  16. * Android生命周期回调方法-销毁
  17. */
  18. @Override
  19. protected void onDestroy() {
  20. AppManager.getAppManager().finishActivity(this);
  21. super.onDestroy();
  22. }
  23. @Override
  24. protected void onResume() {
  25. super.onResume();
  26. overridePendingTransition(android.R.anim.fade_in,
  27. android.R.anim.fade_out);
  28. }
  29. @Override
  30. protected void onPause() {
  31. super.onPause();
  32. }
  33. /**
  34. * 初始化界面
  35. */
  36. private void initView() {
  37. loadViewLayout();
  38. findViewById();
  39. processLogic();
  40. setListener();
  41. }
  42. /**
  43. * find控件
  44. */
  45. protected abstract void findViewById();
  46. /**
  47. * 加载布局
  48. */
  49. protected abstract void loadViewLayout();
  50. /**
  51. * 后台获取数据
  52. */
  53. protected abstract void processLogic();
  54. /**
  55. * 设置监听
  56. */
  57. protected abstract void setListener();

将获取布局,获取View,获取后台数据,设置监听设置为抽象方法,使得子类继承时必须要实现。

子类对抽象方法的实现

  1. @Override
  2. protected void findViewById() {
  3. newsLv = (ListView) this.findViewById(R.id.news_lv);
  4. gallery = (MyGallery) galleryView.findViewById(R.id.gallery);
  5. galleryRl = (RelativeLayout) galleryView.findViewById(R.id.rl_gallery);
  6. bannerTv = (TextView) galleryView.findViewById(R.id.banner_tv);
  7. addMoreBtn = (TextView) addMoreView.findViewById(R.id.btn_add_more);
  8. }
  9. @Override
  10. protected void loadViewLayout() {
  11. setContentView(R.layout.news_list_layout);
  12. galleryView = View.inflate(mContext, R.layout.gallery_layout, null);
  13. addMoreView = View.inflate(mContext, R.layout.add_more, null);
  14. setTitleBarView(false, "资讯", -1, true);
  15. }
  16. @Override
  17. protected void processLogic() {
  18. newsLv.addHeaderView(galleryView);
  19. eduNewsList = new ArrayList<NewsListEntity.News>();
  20. newsLv.addFooterView(addMoreView);
  21. // getTopNewsData();
  22. }
  23. @Override
  24. protected void setListener() {
  25. addMoreBtn.setOnClickListener(this);
  26. newsLv.setOnItemClickListener(new OnItemClickListener() {
  27. @Override
  28. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  29. long arg3) {
  30. Intent detailIntent = new Intent(mContext,
  31. NewsDetailActivity.class);
  32. if (eduNewsList.size() > arg2 - 1) {
  33. detailIntent.putExtra("id", eduNewsList.get(arg2 - 1).Id);
  34. startActivity(detailIntent);
  35. }
  36. }
  37. });

发表评论

表情:
评论列表 (有 0 条评论,268人围观)

还没有评论,来说两句吧...

相关阅读

    相关 AndroidActivity框架

    在安卓应用中,经常需要Activity中经常需要有大量相似的Activity类,这些类往往有相似的结构与功能,因此产生了大量重复代码,为此,以下提供一种方法有效的降低了代码冗