android activity自定義抽象父類
阿新 • • 發佈:2019-02-12
android開發最常用的元件就是activity,但是activity中也有很多常用的方法,幾乎每次建立activity後都需要呼叫的一些方法流程,在此自定義一個父類-BaseActivity,使所有的activity都繼承於這個父類,繼承以後會自動繼承父類的方法,並集成了一些介面跳轉動畫等公共效果,
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
protected Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView();
setContentView(R.layout.activity_base_layout);
initParent();
findViews();
setListensers();
MyApplication myApplication = (MyApplication) getApplication();
myApplication.addTempActivityInBackStack(this );
}
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
// TODO Auto-generated method stub
super.onActivityResult(arg0, arg1, arg2);
}
private void initParent() {
mContext = this;
LinearLayout subCententView = (LinearLayout) this .findViewById(R.id.base_sub_activty_layout);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
View centerView = View.inflate(mContext, setContentView(), null);
subCententView.addView(centerView, layoutParams);
}
protected boolean isShowNoNetworksPrompt() {
return true;
}
/**
* 跳轉到某個Activity
*/
protected void gotoActivity(Context mContext, Class<?> toActivityClass, Bundle bundle) {
Intent intent = new Intent(mContext, toActivityClass);
if (bundle != null) {
intent.putExtras(bundle);
}
mContext.startActivity(intent);
((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.not_exit_push_left_out);//頁面跳轉動畫
}
/**
* 退出到某個Activity
*/
protected void backActivity() {
finish();
overridePendingTransition(R.anim.not_exit_push_left_in, R.anim.push_right_out);//頁面退出動畫
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 所有需要統一處理的onKeyDown寫在這個if裡面
if (isOnKeyDown()) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
backActivity();
}
}
return super.onKeyDown(keyCode, event);
}
protected boolean isOnKeyDown() {
return true;
}
/**
* 載入子類佈局
*/
protected abstract int setContentView();
/**
* 載入控制元件
*/
protected abstract void findViews();
/**
* 設定監聽
*/
protected abstract void setListensers();
@Override
protected void onResume() {
super.onResume();
MyApplication myApplication = (MyApplication) getApplication();
myApplication.setResumeContext(this);
if (JPushInterface.isPushStopped(getApplicationContext())) {
JPushInterface.resumePush(getApplicationContext());
}
}
}
activity_base_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/base_sub_activty_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
not_exit_push_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0%p"
android:interpolator="@android:anim/linear_interpolator"
android:toXDelta="0%p" />
</set>
not_exit_push_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0%p"
android:interpolator="@android:anim/linear_interpolator"
android:toXDelta="0%p" />
</set>
push_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="100%p"
android:interpolator="@android:anim/linear_interpolator"
android:toXDelta="0%p" />
</set>
push_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0%p"
android:interpolator="@android:anim/linear_interpolator"
android:toXDelta="100%p" />
</set>