Java tutorial
/* * Copyright (c) 2015 GuDong * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.xiaoxin.apktools.app; import android.annotation.TargetApi; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import com.xiaoxin.apktools.R; /** * Created by mao on 7/16/15. */ public abstract class BaseActivity extends AppCompatActivity { private Toolbar mToolbar; @Override protected void onCreate(Bundle savedInstanceState) { // ? super.onCreate(savedInstanceState); // ??, true // if (initLayout() > 0) { setContentView(initLayout()); } // ?toolbar initToolBar(); // Android4.4 } private void initToolBar() { mToolbar = (Toolbar) findViewById(R.id.toolbar); if (mToolbar != null) { setSupportActionBar(mToolbar); } } public Toolbar getToolbar() { return mToolbar; } /** * toolbar * @param title * @param showHome ? */ public void setupToolBar(int title, boolean showHome) { setTitle(getString(title)); setDisplayHomeEnable(showHome); } private void setDisplayHomeEnable(boolean showHome) { getSupportActionBar().setDisplayShowHomeEnabled(showHome); getSupportActionBar().setDisplayHomeAsUpEnabled(showHome); } private void setToolbarTitle(String title) { if (mToolbar != null) { mToolbar.setTitle(title); } } protected void setStatusBarColorRes(int res) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setStatusBarColor(res); } else { } } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void setStatusBarColor(int res) { // Window window = getWindow(); // window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); getWindow().setStatusBarColor(ContextCompat.getColor(this, res)); } /** * set layout file * @return res id of layout,if return value less then zero ,it indicate this activity will not set content view */ protected abstract int initLayout(); @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); break; } return super.onOptionsItemSelected(item); } /** * ??Activity?Activity */ public void reload() { Intent intent = getIntent(); overridePendingTransition(0, 0); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); finish(); overridePendingTransition(0, 0); startActivity(intent); } }