首先从实际使用上总结一下activity相关知识(建立在Android5.1以上)
一. 所有app都有的启动页
1.1 清单文件的配置
<!-- 使用自定义theme解决白屏问题 -->
<activity
android:name=".AppStartActivity"
android:theme="@style/Theme.AppStart">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 背景图Theme设置给AppStartActivity-->
<style name="Theme.AppStart" parent="Theme.AppCompat.Light.NoActionBar">
//ic_app_launcher是背景图
<item name="android:windowBackground">@drawable/ic_app_launcher</item>
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
1.2 AppStartActivity
public class AppStartActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
//去掉标题头
requestWindowFeature(Window.FEATURE_NO_TITLE);
//全屏
setFullScreen();
//设置显示的view
setContentView(R.layout.activity_app_start);
//去掉标题栏注意这句一定要写在setContentView()方法的后面
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
/*
* 用于判断这个Activity的启动标志,看它所在的应用是不是从后台跑到前台的。如果是, * 则直接把它finish()掉,
* 然后系统会去Activity启动历史栈查询上一个activity,然后再新建它,所以还原到了我们按home键出去的那个界面。
*/
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish();
}
//检查权限,可以这样,方法在onResume()后执行
getWindow().getDecorView().post(() -> {
Log.d("checkPermission ","onResume后执行");
});
}
/**
* 全屏
*/
public void setFullScreen() {
//设置全屏
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
//透明状态栏
setColor(Color.TRANSPARENT);
}
/**
* 沉浸状态栏
*
* @param color
*/
public void setStatusBar(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
getWindow().setStatusBarColor(color);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
//kill self
android.os.Process.killProcess(android.os.Process.myPid());
}
return super.onKeyDown(keyCode, event);
}
二. 重要的MainActivity
2.1 清单文件的配置
<activity
android:name=".MainActivity"
//横竖屏切换设置
android:configChanges="orientation|screenSize|keyboardHidden"
//启动模式
android:launchMode="singleTask"
//自定义透明theme
android:theme="@style/Theme.windowBackground.transparent"
//隐藏软键盘
android:windowSoftInputMode="stateAlwaysHidden|adjustPan" />
2.2 MainActivity
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
//去掉标题头
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置显示的view
setContentView(R.layout.activity_main);
//去掉标题栏注意这句一定要写在setContentView()方法的后面
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
@Override
public void onClick(View v) {
if (fastClick(v)) {
widgetClick(v);
}
}
/**
* 防止快速点击
*
* @param v
* @return
*/
private long lastClick = 0;
private int lastClickViewId = -1;
private boolean fastClick(View v) {
if (v.getId() == lastClickViewId) {
//点击了同一个控件500ms内无效
if (System.currentTimeMillis() - lastClick <= 500) {
return false;
}
}
lastClickViewId = v.getId();
lastClick = System.currentTimeMillis();
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//在主界面点返回键,回到后台
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return false;
}
return super.onKeyDown(keyCode, event);
}
版权声明:本文为Mr_theSun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。