package com.danxx.mdplayer.base; import android.annotation.TargetApi; import android.os.Build; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.transition.Explode; import android.view.MenuItem; import android.view.Window; import android.widget.Toast; import com.umeng.analytics.MobclickAgent; /** * Created by Danxx on 2016/6/15. * 基本Activity */ public abstract class BaseActivity extends AppCompatActivity { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); getWindow().setEnterTransition(new Explode().setDuration(800)); this.setContentView(this.getLayoutId()); this.initToolbar(savedInstanceState); this.initViews(savedInstanceState); this.initData(); this.initListeners(); } /** * Fill in layout id * * @return layout id */ protected abstract int getLayoutId(); /** * Initialize the view in the layout * * @param savedInstanceState savedInstanceState */ protected abstract void initViews(Bundle savedInstanceState); /** * Initialize the Activity data */ protected abstract void initData(); /** * Initialize the toolbar in the layout * * @param savedInstanceState savedInstanceState */ protected abstract void initToolbar(Bundle savedInstanceState); /** * Initialize the View of the listener */ protected abstract void initListeners(); /********* * Toast * *********/ public void showToast(String msg) { if(msg != null){ Toast.makeText(this,msg ,Toast.LENGTH_SHORT).show(); } } @Override protected void onResume() { super.onResume(); MobclickAgent.onResume(this); } @Override protected void onPause() { super.onPause(); MobclickAgent.onPause(this); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); return true; } return super.onOptionsItemSelected(item); } @Override public void onBackPressed() { super.onBackPressed(); finish(); } /** * 设置标题 * @param strTitle 标题 * @param showHome 显示返回按钮 */ public void setTitle(String strTitle,boolean showHome){ setTitle(strTitle); getSupportActionBar().setDisplayShowHomeEnabled(showHome); getSupportActionBar().setDisplayHomeAsUpEnabled(showHome); } }