Android mvp+retrofit+rxjava 编程实践(一)--BaseActivity

时间:2021-08-20 17:49:13

到现在工作一年半了,大大小小的项目做了几个,最近有点空闲,就总结一下前边做的几个项目,尝试一下新的架构和框架.
MVP也没怎么用过,自己摸索着写了这个文件结构.
Android mvp+retrofit+rxjava 编程实践(一)--BaseActivity

先写BaseActivity

package com.hey.baseapplication.activity;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;

import com.hey.baseapplication.presenter.BasePresenter;

/**
* Created by Hey on 16/12/15.
*/


public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity{
protected T presenter;

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
//锁定竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
initView();

}


@Override
protected void onResume() {
super.onResume();
ActivityManager.getInstance().addActivity(this);
}

@Override
protected void onDestroy() {
presenter.destroy();
ActivityManager.getInstance().removeActivity(this);
super.onDestroy();
}

protected abstract void initPresenter();


protected void initView(){
initPresenter();
findView();
initData();
setListener();

}


protected abstract void findView();
protected abstract void initData();
protected abstract void setListener();

/**
* 获取屏幕的宽度(px)
* @return width
*/

public int getScreenWidth(){
DisplayMetrics displayMetrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.widthPixels;
}

/**
* 获取屏幕的高度(px)
* @return height
*/

public int getScreenHeight(){
DisplayMetrics displayMetrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
}

/**
* 获取网络连接状态
* @return
*/

public int getNetWorkState() {
ConnectivityManager manager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager.getActiveNetworkInfo()!=null&&manager.getActiveNetworkInfo().isAvailable()) {
return manager.getActiveNetworkInfo().getType();
}
return 0;
}
protected <T extends View> T f(int id) {
return (T) super.findViewById(id);
}

public void showToast(String message){
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}
//沉浸式状态栏
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus&& Build.VERSION.SDK_INT>=19){
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
}

主要写了几通用的方法,以后用到了还以再添加.
ActivityManager 用来储存打开的activity

package com.hey.baseapplication.activity;

import android.app.Activity;
import android.content.Context;

import java.util.ArrayList;
import java.util.Stack;

/**
* Created by Hey on 2016/5/23.
*/


public class ActivityManager {
private static ArrayList<Activity> activityStack;
private static ActivityManager instance;
static {
instance = new ActivityManager();
}

public static ActivityManager getInstance() {
return instance;
}

/**
* 添加指定Activity到集合
*/

public void addActivity(Activity activity){
if(activityStack==null){
activityStack=new ArrayList<>();
}
activityStack.add(activity);
}
/**
* 结束指定的Activity
*/

public void removeActivity(Activity activity){
if(activity!=null){
activityStack.remove(activity);
}
}

/**
* 结束全部的Activity
*/

public void finishAllActivity(){
for (int i = 0, size = activityStack.size(); i < size; i++){
if (null != activityStack.get(i)){
activityStack.get(i).finish();
}
}
activityStack.clear();
}
/**
* 退出应用程序
*/

public void AppExit(Context context) {
try {
finishAllActivity();
android.app.ActivityManager activityMgr= (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
activityMgr.restartPackage(context.getPackageName());
System.exit(0);
} catch (Exception e) { }
}
}

这里今天看了几篇文章说最简洁的单例模式可以利用枚举这样写,用法为ActivityManager.instance.method();

package com.hey.baseapplication.activity;

import android.app.Activity;
import android.content.Context;

import java.util.ArrayList;
import java.util.Stack;

/**
* Created by Hey on 2016/5/23.
*/


public enum ActivityManager {
instance;
private static ArrayList<Activity> activityStack;
/**
* 添加指定Activity到集合
*/

public void addActivity(Activity activity){
if(activityStack==null){
activityStack=new ArrayList<>();
}
activityStack.add(activity);
}
/**
* 结束指定的Activity
*/

public void removeActivity(Activity activity){
if(activity!=null){
activityStack.remove(activity);
}
}

/**
* 结束全部的Activity
*/

public void finishAllActivity(){
for (int i = 0, size = activityStack.size(); i < size; i++){
if (null != activityStack.get(i)){
activityStack.get(i).finish();
}
}
activityStack.clear();
}
/**
* 退出应用程序
*/

public void AppExit(Context context) {
try {
finishAllActivity();
android.app.ActivityManager activityMgr= (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
activityMgr.restartPackage(context.getPackageName());
System.exit(0);
} catch (Exception e) { }
}
}