Activity的生命周期(三)——结合Activity的生命周期理解子类继承父类预加载数据

父类代码:

/**   
 * @author 贾涛   
 * @date 2016-2-27 上午5:59:49 
 */
public class BaseActivity extends Activity {

	private String TAG = "BaseActivity";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.e(TAG, "onCreate");
	}
	
	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		Log.e(TAG, "setContentView");
		setData();
		Log.e(TAG, "setData"+"我可以通过这个方法在父类中预加载数据");
	}

	public void setData() {
		Log.e(TAG, "setData返回的数据是woshijiatao");
		System.out.println("woshijiatao");
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.e(TAG, "onStart");
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		Log.e(TAG, "onRestart");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.e(TAG, "onResume");
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Log.e(TAG, "onPause");
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		Log.e(TAG, "onStop");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.e(TAG, "onDestroy");
	}
}

子类代码:

public abstract class BaseActivity extends Activity {
	protected final String TAG = this.getClass().getSimpleName();
	protected Handler mHandler;
	protected boolean isLive;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		((MoyiApplication) getApplication()).add(this);
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		isLive = true;
		Constants.screenW = metrics.widthPixels;
		Constants.screenH = metrics.heightPixels;
		getWindow().setWindowAnimations(R.style.ActivityAnimation);
	}

	@Override
	public void setContentView(int layoutResID) {
		// TODO Auto-generated method stub
		super.setContentView(layoutResID);
		ViewUtils.inject(this);
//		if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this))
//			return;
		initView();
		setListen();
	}

	protected void setListen() {
	};

	protected void initView() {
	};

	protected void setLeftIC(boolean show, int resid) {
		ImageView view = (ImageView) findViewById(R.id.title_iv_left);
		if (show) {
			view.setVisibility(View.VISIBLE);
			view.setImageResource(resid);
		} else {
			view.setVisibility(View.GONE);
		}
	}

	protected void setRigthIC(boolean show, int resid) {
		ImageView view = (ImageView) findViewById(R.id.title_iv_rigth);
		if (show) {
			view.setVisibility(View.VISIBLE);
			view.setImageResource(resid);
		} else {
			view.setVisibility(View.GONE);
		}
	}

	protected void setCenterIC(boolean show, int resid) {
		ImageView view = (ImageView) findViewById(R.id.title_iv_center);
		if (show) {
			view.setVisibility(View.VISIBLE);
			view.setImageResource(resid);
		} else {
			view.setVisibility(View.GONE);
		}
	}

	protected void setCenterHWIC(boolean show, int resid, Animation anim) {
		ImageView view = (ImageView) findViewById(R.id.title_iv_center);
		view.setAnimation(anim);
		view.setVisibility(View.VISIBLE);
		view.setImageResource(resid);
	}

	protected void setCenterHWICHidden(boolean show) {
		ImageView view = (ImageView) findViewById(R.id.title_iv_center);
		view.setAnimation(null);
		view.setVisibility(View.GONE);
	}

	protected void setLeftText(boolean show, String text) {
		TextView view = (TextView) findViewById(R.id.title_tv_left);
		if (show) {
			view.setVisibility(View.VISIBLE);
			view.setText(text);
		} else {
			view.setVisibility(View.GONE);
		}
	}

	protected void setLeftText1(boolean show, int text) {
		TextView view = (TextView) findViewById(R.id.title_tv_left);
		if (show) {
			view.setVisibility(View.VISIBLE);
			view.setText(text);
		} else {
			view.setVisibility(View.GONE);
		}
	}

	protected void setRightText(boolean show, String text) {
		TextView view = (TextView) findViewById(R.id.title_tv_rigth);
		if (show) {
			view.setVisibility(View.VISIBLE);
			view.setText(text);
		} else {
			view.setVisibility(View.GONE);
		}
	}

	protected void setRightText1(boolean show, int text) {
		TextView view = (TextView) findViewById(R.id.title_tv_rigth);
		if (show) {
			view.setVisibility(View.VISIBLE);
			view.setText(text);
		} else {
			view.setVisibility(View.GONE);
		}
	}

	protected void setText(boolean show, String text) {
		TextView view = (TextView) findViewById(R.id.title_tv_title);
		if (show) {
			view.setVisibility(View.VISIBLE);
			view.setText(text);
		} else {
			view.setVisibility(View.GONE);
		}
	}

	protected void setText1(boolean show, int text) {
		TextView view = (TextView) findViewById(R.id.title_tv_title);
		if (show) {
			view.setVisibility(View.VISIBLE);
			view.setText(text);
		} else {
			view.setVisibility(View.GONE);
		}
	}

	protected void settitleBG(int color) {
		RelativeLayout view = (RelativeLayout) findViewById(R.id.title_bg);
		view.setBackgroundColor(color);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);

	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		((MoyiApplication) getApplication()).del(this);
		isLive = false;
	}

}

activity_one.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.imooc.activitystudy.OneActivity" >

    <TextView
        android:id="@+id/tv_actName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

【Activity创建】


你可能感兴趣的:(Activity的生命周期(三)——结合Activity的生命周期理解子类继承父类预加载数据)