BaseActivity基类里面的UI设计通常有以下一个部分:1.title的控制 2.加载的控制 3.主体内容的显示 4.没网或是没数据时的异常显示
另外还有Activity跳转的控制。
先看看我的基类的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<com.jingubank.main.view.TitleBar
android:id="@+id/base_fragment_title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
android:id="@+id/base_fragment_net_error"
layout="@layout/common_neterror"
android:visibility="gone" />
<include
android:id="@+id/base_fragment_empty"
layout="@layout/common_empty"
android:visibility="gone" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/base_fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:id="@+id/base_fragment_loading"
layout="@layout/common_loading_pro"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
BaseActivity
package com.example.main.base;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.main.R;
import com.example.main.common.app.AppManager;
import com.example.main.finance.other.ChackGesturePswActivity;
import com.example.main.utils.ActivityAnim;
import com.example.main.utils.NetWorkUtils;
import com.example.main.utils.ResourcesUtils;
import com.example.main.utils.StatusBarUtil;
import com.example.main.view.TitleBar;
public abstract class BaseActivity extends AppCompatActivity {
protected Context mContext;
private InputMethodManager manager = null;
protected BaseApplication application;
protected TitleBar viewTitleBar;
private FrameLayout viewContent;
private LinearLayout viewEmpty, viewNetError;
private View viewLoading;
private int viewContentId;//viewPageLayoutId
protected View rootView;
private TextView base_neterror_tv;
private TextView base_empty_tv;
long currentTime = 0;
@Override
protected void onDestroy() {
super.onDestroy();
AppManager.getAppManager().removeActivity(this);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mContext = this;
this.viewContentId = onCreateViewId();
try {
getExras();
} catch (Exception e) {
e.printStackTrace();
}
setContentView(R.layout.base_page_title);
initViewPage();
setListener();
titleBarSet(viewTitleBar);
if (NetWorkUtils.isConnected(mContext)) {
viewContent.setVisibility(View.VISIBLE);
} else {
showViewNetError(true, null);
}
AppManager.getAppManager().addActivity(this);//推入栈中
}
/**
* 设置广播接收,区别操作
*/
protected int whereId;
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
return super.onCreateView(parent, name, context, attrs);
}
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
return super.onCreateView(name, context, attrs);
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
setStatusBar();
}
/**
* 设置状态栏
*/
protected void setStatusBar() {
StatusBarUtil.setColor(this, getResources().getColor(R.color.transparent));
}
@Override
public void onResume() {
super.onResume();
fromNetGetData();
}
@Override
public void onPause() {
super.onPause();
}
/**
* 设置标题栏
*
* @param titleBar
*/
protected abstract void titleBarSet(TitleBar titleBar);
/**
* 设置页面布局
*
* @return
*/
protected abstract int onCreateViewId();
/**
* 设置页面布局实例化
*
* @return
*/
protected abstract void onCreateViewContent(View view);
/**
* 请求网络数据
*/
protected abstract void fromNetGetData();
/**
* 本地传参
*/
protected abstract void getExras();
/**
* 监听事件
*/
protected abstract void setListener();
/**
* 加载框,是否显示
*
* @param show
*/
protected void showViewLoading(boolean show) {
if (show) {
viewLoading.setVisibility(View.VISIBLE);
} else {
viewLoading.setVisibility(View.GONE);
}
}
/**
* 空数据,是否显示
*
* @param show
*/
protected void showEmptyView(boolean show) {
if (show) {
viewEmpty.setVisibility(View.VISIBLE);
} else {
viewEmpty.setVisibility(View.GONE);
}
}
/**
* 网络错误,是否显示
*
* @param show
*/
protected void showViewNetError(boolean show, View.OnClickListener onClickListener) {
if (show) {
viewNetError.setOnClickListener(onClickListener);
viewNetError.setVisibility(View.VISIBLE);
} else {
viewNetError.setVisibility(View.GONE);
}
}
/**
* 初始化页面内容
*/
private void initViewPage() {
try {
viewTitleBar = (TitleBar) findViewById(R.id.base_fragment_title_bar);
viewEmpty = (LinearLayout) findViewById(R.id.base_fragment_empty);
viewNetError = (LinearLayout) findViewById(R.id.base_fragment_net_error);
viewContent = (FrameLayout) findViewById(R.id.base_fragment_content);
viewLoading = findViewById(R.id.base_fragment_loading);
base_neterror_tv = (TextView) findViewById(R.id.base_neterror_tv);
base_empty_tv = (TextView) findViewById(R.id.base_empty_tv);
View viewPage = View.inflate(mContext, viewContentId, null);
rootView = findViewById(R.id.root_view);
viewContent.addView(viewPage);
onCreateViewContent(viewPage);
viewEmpty.setVisibility(View.GONE);
viewNetError.setVisibility(View.GONE);
} catch (Exception e) {
showViewLoading(false);
e.printStackTrace();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
/**
* 点击空白处隐藏软键盘
*/
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
if (manager == null) {
manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
}
manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
return super.onTouchEvent(event);
}
@Override
public BaseApplication getApplicationContext() {
return (BaseApplication) super.getApplicationContext();
}
@Override
public void onBackPressed() {
ActivityAnim.finshActivity(this);
}
}
公共的Title common_title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="51dp"
android:background="@color/cffffff"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/title_search_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingLeft="11dp"
android:paddingRight="13dp"
android:visibility="gone">
<TextView
android:id="@+id/title_bar1_left"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:drawableLeft="@mipmap/icon_location"
android:gravity="center_vertical"
android:minWidth="15dp"
android:scaleType="centerInside"
android:text="全国"
android:textSize="14sp" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/title_bar1_right"
android:layout_toRightOf="@+id/title_bar1_left">
<EditText
android:id="@+id/title_bar1_edt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/bg_search_shape_white"
android:drawableLeft="@mipmap/search_icon_find"
android:drawablePadding="5dp"
android:enabled="true"
android:gravity="center_vertical"
android:hint="搜索商品"
android:maxLines="1"
android:paddingLeft="15dp"
android:paddingRight="25dp"
android:textSize="14sp" />
<ImageView
android:id="@+id/title_bar1_clear"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center_vertical|right"
android:layout_marginRight="5dp"
android:src="@mipmap/icon_close" />
</FrameLayout>
<TextView
android:id="@+id/title_bar1_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center"
android:minWidth="50dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="取消"
android:textColor="#000000"
android:textSize="14sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<TextView
android:id="@+id/title_bar_back"
android:layout_width="110dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:drawableLeft="@drawable/ic_title_back"
android:ellipsize="end"
android:gravity="center_vertical"
android:minWidth="45dp"
android:singleLine="true"
android:visibility="visible" />
<TextView
android:id="@+id/title_bar_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:gravity="center"
android:text="@string/app_name_lib"
android:textColor="#000000"
android:textSize="16sp" />
<LinearLayout
android:id="@+id/title_bar_right"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:minWidth="45dp"
android:orientation="horizontal">
<TextView
android:id="@+id/title_bar_set"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawableRight="@mipmap/title_icon_add"
android:gravity="center"
android:minWidth="50dp"
android:paddingRight="7dp"
android:text=""
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="@+id/title_bar_edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minWidth="50dp"
android:text="编辑"
android:textColor="#000000"
android:textSize="14sp" />
</LinearLayout>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#e3e3e3" />
</LinearLayout>
TitleBar
package com.example.main.view;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.main.R;
public class TitleBar extends LinearLayout implements View.OnClickListener {
/**
* 文字标题栏
*/
private TextView viewTxtName, viewTxtBack;
private TextView viewTxtSet, viewTxtEdt;
/**
* 搜索栏,UI控件
*/
private TextView v1TvLeft,v1TvRight;
private ImageView v1ImgClear;
private EditText v1EdtSearch;
public TitleBar(Context context) {
this(context, null);
}
public TitleBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
/**
* 初始化文字标题栏
*/
private void initView() {
setOrientation(LinearLayout.VERTICAL);
View.inflate(getContext(), R.layout.common_title_bar, this);
viewTxtName = (TextView) findViewById(R.id.title_bar_name);
viewTxtBack = (TextView) findViewById(R.id.title_bar_back);
viewTxtSet = (TextView) findViewById(R.id.title_bar_set);
viewTxtEdt = (TextView) findViewById(R.id.title_bar_edt);
viewTxtBack.setOnClickListener(this);
viewTxtSet.setOnClickListener(this);
viewTxtEdt.setOnClickListener(this);
viewTxtSet.setVisibility(View.GONE);
viewTxtEdt.setVisibility(View.GONE);
}
public final static int clickBack = 1;//返回
public final static int clickEdt = 2;
public final static int clickSet = 3;
public final static int click1Edt= 4;
public final static int click1Left = 5;
public final static int click1Right = 6;
public final int click1Clear = 7;
public enum TitleType {
set, back, edt;
}
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.title_bar_back) {
if (titleBarClickImpl != null) {
titleBarClickImpl.titleOnClick(clickBack);
}
} else if (i == R.id.title_bar_set) {
if (titleBarClickImpl != null) {
titleBarClickImpl.titleOnClick(clickSet);
}
} else if (i == R.id.title_bar_edt) {
if (getTitleBarClickImpl() != null) {
titleBarClickImpl.titleOnClick(clickEdt);
}
} else if (i == R.id.title_bar1_left) {
if (getTitleBarClickImpl() != null) {
titleBarClickImpl.titleOnClick(click1Left);
}
} else if (i == R.id.title_bar1_clear) {
if (v1EdtSearch!=null){
v1EdtSearch.setText("");
}
// titleBarClickImpl.titleOnClick(click1Clear);
}else if (i == R.id.title_bar1_edt) {
if (getTitleBarClickImpl() != null) {
titleBarClickImpl.titleOnClick(click1Edt);
}
} else if (i == R.id.title_bar1_right) {
if (getTitleBarClickImpl() != null) {
titleBarClickImpl.titleOnClick(click1Right);
}
}
}
TitleBarClick titleBarClickImpl;
public TitleBarClick getTitleBarClickImpl() {
return titleBarClickImpl;
}
public void setTitleBarClickImpl(TitleBarClick titleBarClickImpl) {
this.titleBarClickImpl = titleBarClickImpl;
}
/**
* 标题栏,点击事件监听接口
*/
public interface TitleBarClick {
/**
* @param titleType 1:Back,2:edt,3:set,4:搜索,5位置,6搜索取消
*/
void titleOnClick(int titleType);
}
/**
* 设置原返回键图标,及文字 0不设置图标
*/
public void setLeftView(int drwId,String name){
setTextDrwName(viewTxtBack,drwId);
viewTxtBack.setText(textDealWithStr(name));
}
/**
* 设置原设置键图标,及文字 0不设置图标
*/
public void setRightSet(int drwId,String name){
setTextDrwNameRight(viewTxtSet,drwId);
viewTxtSet.setText(textDealWithStr(name));
}
/**
* 设置原设置键图标,及文字 0不设置图标
*/
public void setRightSetColor(String name,int resId){
viewTxtSet.setText(textDealWithStr(name));
setTextDrwNameRight(viewTxtSet,0);
viewTxtSet.setTextColor(getResources().getColor(resId));
}
/**
* 设置原编辑键图标,及文字 0不设置图标
*/
public void setRightEdit(int drwId,String name){
setTextDrwNameRight(viewTxtEdt,drwId);
viewTxtEdt.setText(textDealWithStr(name));
}
private void setTextDrwName(TextView textView,int drawId){
Drawable drawable=null;
if (drawId!=0){
drawable=getResources().getDrawable(drawId);
drawable.setBounds( 0, 0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
}
textView.setCompoundDrawables(drawable, null, null, null);
}
//设置右侧
private void setTextDrwNameRight(TextView textView,int drawId){
Drawable drawable=null;
if (drawId==101){
textView.setTextColor(Color.parseColor("#C39F8A"));
}else if(drawId!=101&&drawId!=0){
drawable=getResources().getDrawable(drawId);
drawable.setBounds( 0, 0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
}
textView.setCompoundDrawables(null, null,drawable, null);
}
public void setTitleName(String name) {
viewTxtName.setText(textDealWithStr(name));
}
/**
* 设置搜索标题栏,显示搜索栏
*
* @param clickEdt 是否可编辑
*/
public void setSearchTitle(boolean clickEdt, boolean showRight, boolean showLeft) {
View viewSearch = findViewById(R.id.title_search_layout);
viewSearch.setVisibility(View.VISIBLE);
v1EdtSearch = (EditText) findViewById(R.id.title_bar1_edt);
v1ImgClear= (ImageView) findViewById(R.id.title_bar1_clear);
v1ImgClear.setVisibility(View.GONE);
v1ImgClear.setOnClickListener(this);
v1TvRight = (TextView) findViewById(R.id.title_bar1_right);
if (showRight) {
v1TvRight.setVisibility(View.VISIBLE);
v1TvRight.setOnClickListener(this);
} else {
v1TvRight.setVisibility(View.GONE);
}
v1TvLeft = (TextView) findViewById(R.id.title_bar1_left);
if (showLeft) {
v1TvLeft.setVisibility(View.VISIBLE);
v1TvLeft.setOnClickListener(this);
} else {
v1TvLeft.setVisibility(View.GONE);
}
if (clickEdt) {
v1EdtSearch.setEnabled(true);
} else {
v1EdtSearch.setEnabled(true);
v1EdtSearch.setOnClickListener(this);
}
}
/**
* 设置输入框监听观察者
* @param textWatcher
*/
public void setSearchEdtView(TextWatcher textWatcher,String hint){
if (v1EdtSearch!=null) {
v1EdtSearch.addTextChangedListener(textWatcher);
// v1EdtSearch.setHint(hint);
}
}
/**
* 选取搜搜内容
* @return
*/
public String getSearchTxt(){
if (v1EdtSearch!=null) {
return v1EdtSearch.getText().toString();
}else{
return "";
}
}
/**
* 设置搜索框内容是否可清除 图标
* @param show
*/
public void setSearchClear(boolean show){
if (v1ImgClear!=null) {
if (show){
v1ImgClear.setVisibility(View.VISIBLE);
}else{
v1ImgClear.setVisibility(View.GONE);
}
}
}
/**
* 设置搜索显示内容
* @param search
*/
public void setSearchText(String search){
if (v1EdtSearch!=null) {
v1EdtSearch.setText(search);
}
}
/**
* 设置搜索标题栏,左侧定位按钮显示
* @param name
* @param drawableId
*/
public void setSearchLeftTv(String name,int drawableId) {
if (v1TvLeft == null) {
v1TvLeft = (TextView) findViewById(R.id.title_bar1_left);
}
v1TvLeft.setVisibility(View.VISIBLE);
setTextDrwName(v1TvLeft,drawableId);
v1TvLeft.setText(textDealWithStr(name));
}
/**
* 设置搜索标题栏,右侧定位按钮显示
* @param name
* @param drawableId 0不显示图标
*/
public void setSearchRightTv(String name,int drawableId) {
if (v1TvRight == null) {
v1TvRight = (TextView) findViewById(R.id.title_bar1_right);
}
v1TvRight.setVisibility(View.VISIBLE);
setTextDrwName(v1TvRight,drawableId);
v1TvRight.setText(textDealWithStr(name));
}
/**
* @param nameId
*/
public void setTitleName(int nameId) {
viewTxtName.setText(nameId);
}
public void setTitleBackName(String name) {
viewTxtBack.setText(textDealWithStr(name));
}
public void setTitleEdtName(String name) {
viewTxtEdt.setText(textDealWithStr(name));
}
//处理字符串 不是null
private String textDealWithStr(String name){
return TextUtils.isEmpty(name)?"":name;
}
/**
* 普通文字标题栏
* @param back 是否显示返回键
* @param edt 是否显示编辑键
* @param set 是否显示设置键
*/
public void setShowIcon(boolean back, boolean edt, boolean set) {
if (back) {
viewTxtBack.setVisibility(View.VISIBLE);
} else {
viewTxtBack.setVisibility(View.GONE);
}
if (edt) {
viewTxtEdt.setVisibility(View.VISIBLE);
} else {
viewTxtEdt.setVisibility(View.GONE);
}
if (set) {
viewTxtSet.setVisibility(View.VISIBLE);
} else {
viewTxtSet.setVisibility(View.GONE);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/common_toast_bg"
android:gravity="center"
android:minWidth="110dp"
android:orientation="vertical"
android:padding="10dp">
<ProgressBar
style="@style/mProgress_circle"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:id="@+id/loading_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:text="请稍后"
android:textColor="@color/white"
android:textSize="@dimen/textsize_9" />
</LinearLayout>
</LinearLayout>
drawable/common_toast_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#303030" />
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
<!--旋转进度条-->
<style name="mProgress_circle">
<item name="android:indeterminateDrawable">@drawable/progress_circle_shape</item>
<item name="android:minWidth">25dp</item>
<item name="android:minHeight">25dp</item>
<item name="android:maxWidth">60dp</item>
<item name="android:maxHeight">60dp</item>
</style>
转的环形圈圈 drawable/progress_circle_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotY="50%"
android:toDegrees="360">
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="10"
android:useLevel="false">
<gradient
android:centerColor="#C39F8A"
android:centerY="0.50"
android:endColor="#C39F8A"
android:startColor="#FFFFFF"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
我们的类集成BaseActivity,方便之处列举:
1.设置标题
@Override
protected void titleBarSet(TitleBar titleBar) {
titleBar.setTitleName("存管开户");
titleBar.setTitleBarClickImpl(new TitleBar.TitleBarClick() {
@Override
public void titleOnClick(int titleType) {
switch (titleType) {
case TitleBar.clickBack:
SharePreferenceUtil.setValue(getApplicationContext(), UserConfig.HAS_KAIHU, true);
ActivityAnim.finshActivity(FinanceKaiHuActivity.this);
break;
}
}
});
}
showViewLoading(true);
showViewLoading(false);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppManager.getAppManager().addActivity(this);//推入栈中
}
@Override
protected void onDestroy() {
super.onDestroy();
AppManager.getAppManager().removeActivity(this);
}
AppManager实现Activity堆栈管理
package com.jingubank.main.common.app;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.Intent;
import com.jingubank.main.finance.index.IndexActivity;
import java.util.List;
import java.util.Stack;
public class AppManager {
public static Stack<Activity> activityStack;
private static AppManager instance;
private long exitTime = 0;
public AppManager() {
}
public static AppManager getAppManager() {
if (instance == null) {
instance = new AppManager();
}
return instance;
}
// 退出栈顶Activity,即结束指定的Activity
public void finishActivity(Activity activity) {
if (activity != null) {
activity.finish();
activityStack.remove(activity);
activity = null;
}
}
// 获得当前栈顶Activity
public Activity currentActivity() {
Activity activity = null;
if (null != activityStack && 0 != activityStack.size()) {
activity = activityStack.lastElement();
}
return activity;
}
// 将当前Activity推入栈中
public void addActivity(Activity activity) {
if (activityStack == null) {
activityStack = new Stack<Activity>();
}
activityStack.add(activity);
}
// 将当前Activity移出栈中
public void removeActivity(Activity activity) {
if (activity != null) {
activityStack.remove(activity);
}
}
// 除了某个Activity,退出栈中其它所有Activity
public void popAllActivityExceptOne(Class<?> cls) {
if (null == activityStack || activityStack.size() == 0) {
return;
}
int len = activityStack.size();
for (int i = len - 1; i >= 0; i--) {
Activity activity = activityStack.get(i);
if (activity == null || (null != cls && activity.getClass().equals(cls))) {
continue;
}
finishActivity(activity);
}
}
// 退出栈中当前Activity
public void popUpActivity(Class<?> cls) {
if (null == activityStack || activityStack.size() == 0) {
return;
}
int len = activityStack.size();
for (int i = len - 1; i >= 0; i--) {
Activity activity = activityStack.get(i);
if (activity == null)
continue;
if (activity.getClass().equals(cls)) {
finishActivity(activity);
break;
}
}
}
/**
* 结束所有activity
*/
public void finishAllActivities() {
for (Activity activity : activityStack) {
activity.finish();
}
activityStack.clear();
}
public void gotoMainActivity(Context context) {
popAllActivityExceptOne(IndexActivity.class);
Intent intent = new Intent();
intent.setAction("GO_BACK_MAIN_PAGE");
context.sendBroadcast(intent);
}
public boolean isBackground(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
if (appProcess.importance > RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE) {
// Log.i("后台", appProcess.processName);
return true;
} else {
// System.out.println("appProcess.importance==" + appProcess.importance);
// Log.i("前台", appProcess.processName);
return false;
}
}
}
return true;
}
}
4.Activity跳转工具类,带有动画
package com.example.main.utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.example.main.R;
import java.util.Map;
/**
* Created by MLing on 14/12/12.
*/
public class ActivityAnim {
private static Intent intent = null;
/**
* @param startactivity
* @param finshActivity Activity间跳转
* 实现动画
*/
public static void intentActivityForMap(Activity startactivity, Class<?> finshActivity, Map<String, String> map) {
if (intent == null) {
intent = new Intent();
}
intent.setClass(startactivity, finshActivity);
if (map != null) { //处理
for (String o : map.keySet()) {
intent.putExtra(o, map.get(o));
}
}
startactivity.startActivity(intent);
startactivity.overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
/**
* @param context
* @param cls Activity间跳转
* 实现动画
*/
public static void intentActivity(Context context, Class<?> cls) {
if (intent == null) {
intent = new Intent();
}
if (cls != null) {
intent.setClass(context, cls);
}
context.startActivity(intent);
((Activity) context).overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
/**
* Activity间跳转,加左右动画
*
* @param context
* @param cls 目标Activity
* @param bundle 数据参数,没有参数null
*/
public static void intentActivity(Context context, Class<?> cls, Bundle bundle) {
if (intent == null) {
intent = new Intent();
}
if (cls != null) {
intent.setClass(context, cls);
}
if (bundle != null) {
intent.putExtras(bundle);
}
context.startActivity(intent);
((Activity) context).overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
public static void intentActivity(Activity startactivity, Intent intent) {
if (intent == null) {
return;
}
startactivity.startActivity(intent);
startactivity.overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
/**
* 设置Activity返回接收数据 配套 setResult()使用
*
* @param startactivity
* @param intent
*/
public static void startActivityForResult(Activity startactivity, Intent intent, int intentCode) {
if (intent == null) {
return;
}
startactivity.startActivityForResult(intent, intentCode);
startactivity.overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
public static void setResult(Activity startactivity, Intent intent, int result) {
if (intent == null) {
return;
}
startactivity.setResult(result, intent);
startactivity.finish();
startactivity.overridePendingTransition(R.anim.right_in, R.anim.right_out);
}
public static void startResultActivity(Activity startactivity, Class<?> finshActivity, Map<String, String> map, int requestCode) {
if (intent == null) {
intent = new Intent();
}
intent.setClass(startactivity, finshActivity);
if (map != null) { //处理
for (String o : map.keySet()) {
intent.putExtra(o, map.get(o));
}
}
startactivity.startActivityForResult(intent, requestCode);
startactivity.overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
/**
* @param finshActivity 结束当前activity的动画
*/
public static void finshActivity(Activity finshActivity) {
finshActivity.finish();
finshActivity.overridePendingTransition(R.anim.right_in, R.anim.right_out);
}
// /**
// * @param finshActivity 结束当前程序的动画
// */
// public static void finshActivity(Activity finshActivity) {
// finshActivity.finish();
// }
/**
* @param finshActivity 结束当前activity的动画
*/
public static void homeActivity(Activity finshActivity) {
finshActivity.finish();
finshActivity.overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
/**
* @param finshActivity 结束当前activity的动画
*/
public static void popActivity(Activity finshActivity) {
finshActivity.finish();
finshActivity.overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.slide_out_to_bottom);
}
/**
* 参数序列化
*
* @param strings 参数
* @return 返回序列化之后的String[]数组
*/
public String[] initPropertys(String... strings) {
if (strings[0].equals(""))
return new String[0];
return strings;
}
private static void intent(Intent intent, String[] key, String[] value) {
for (int i = 0; i < key.length; i++) {
intent.putExtra(key[i], value[i]);
}
}
/**
* @param startactivity
* @param finshActivity Activity间跳转
* 实现动画
*/
public static void intentActivityUp(Activity startactivity, Class<?> finshActivity, Map<String, String> map) {
if (intent == null) {
intent = new Intent();
}
intent.setClass(startactivity, finshActivity);
if (map != null) { //处理
for (String o : map.keySet()) {
intent.putExtra(o, map.get(o));
}
}
startactivity.startActivity(intent);
startactivity.overridePendingTransition(R.anim.down_in, R.anim.down_out);
}
/**
* @param finshActivity 结束当前activity的动画
*/
public static void finshActivityDowm(Activity finshActivity) {
finshActivity.finish();
finshActivity.overridePendingTransition(R.anim.down_in, R.anim.down_out);
}
}
right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%"
android:toXDelta="0"
android:duration="200" />
</set>
right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0"
android:toXDelta="100%"
android:duration="200" />
</set>
down_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%"
android:toYDelta="0"
android:duration="400"/>
</set>
down_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0"
android:toYDelta="-100%"
android:duration="400"/>
</set>
要保证Activity切换动画一致,需要在我们点击按钮,前进,后退Activity的地方都是用这个工具类,实体键Back 来finishActivity也要有我们规定的动画
所以在BaseActivity里面重写了onBackPressed:
@Override
public void onBackPressed() {
ActivityAnim.finshActivity(this);
}