Skip to content

Commit 7284292

Browse files
committed
1:修复新闻页面点击回退键无效的bug。
1 parent e1dc64d commit 7284292

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

lib_common/src/main/java/com/guiying/common/base/BaseActionBarActivity.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,39 @@
2222
*/
2323
public abstract class BaseActionBarActivity extends BaseActivity {
2424

25-
protected abstract
26-
@StringRes
27-
int setTitleId();
25+
/*默认的ActionBar*/
26+
protected ActionBar mActionBar;
2827

29-
@Override
30-
protected void onCreate(Bundle savedInstanceState) {
31-
super.onCreate(savedInstanceState);
32-
//标题栏设置
33-
ActionBar actionBar = getSupportActionBar();
34-
if (actionBar != null) {
35-
actionBar.setDisplayHomeAsUpEnabled(true);
36-
actionBar.setHomeButtonEnabled(true);
37-
actionBar.setTitle(setTitleId());
38-
}
39-
}
28+
/**
29+
* 设置默认标题id
30+
*
31+
* @return 标题id
32+
*/
33+
@StringRes
34+
protected abstract int setTitleId();
4035

4136

4237
/**
4338
* 更新标题
4439
*
45-
* @param title 标题
40+
* @param title String标题
4641
*/
4742
protected void setTitle(String title) {
48-
ActionBar actionBar = getSupportActionBar();
49-
if (actionBar != null) {
50-
actionBar.setTitle(title);
43+
if (mActionBar != null) {
44+
mActionBar.setTitle(title);
5145
}
5246
}
5347

54-
5548
@Override
56-
public boolean onSupportNavigateUp() {
57-
onBackPressed();
58-
return true;
49+
protected void onCreate(Bundle savedInstanceState) {
50+
super.onCreate(savedInstanceState);
51+
//标题栏设置
52+
mActionBar = getSupportActionBar();
53+
if (mActionBar != null) {
54+
mActionBar.setDisplayHomeAsUpEnabled(true);
55+
mActionBar.setHomeButtonEnabled(true);
56+
mActionBar.setTitle(setTitleId());
57+
}
5958
}
59+
6060
}
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.guiying.common.base;
22

3-
import android.content.Intent;
43
import android.os.Bundle;
54
import android.support.annotation.IdRes;
65
import android.support.v7.app.ActionBar;
@@ -18,11 +17,25 @@
1817
public abstract class BaseActivity extends AppCompatActivity {
1918

2019
/**
21-
* 处理Intent,防止开发人员没做Intent判空
20+
* Setup the toolbar.
21+
*
22+
* @param toolbar toolbar
23+
* @param hideTitle 是否隐藏Title
2224
*/
23-
protected void handleIntent(Intent intent) {
25+
protected void setupToolBar(Toolbar toolbar, boolean hideTitle) {
26+
setSupportActionBar(toolbar);
27+
ActionBar actionBar = getSupportActionBar();
28+
if (actionBar != null) {
29+
actionBar.setDisplayHomeAsUpEnabled(true);
30+
actionBar.setDisplayShowHomeEnabled(true);
31+
if (hideTitle) {
32+
//隐藏Title
33+
actionBar.setDisplayShowTitleEnabled(false);
34+
}
35+
}
2436
}
2537

38+
2639
/**
2740
* 封装的findViewByID方法
2841
*/
@@ -35,27 +48,19 @@ protected void handleIntent(Intent intent) {
3548
protected void onCreate(Bundle savedInstanceState) {
3649
super.onCreate(savedInstanceState);
3750
BaseApplication.getIns().addActivity(this);
38-
//强制在基类Intent判空
39-
if (null != getIntent()) {
40-
handleIntent(getIntent());
41-
}
4251
}
4352

44-
protected void setToolbar(Toolbar toolbar, String title) {
45-
setSupportActionBar(toolbar);
46-
ActionBar actionBar = getSupportActionBar();
47-
if (null != actionBar) {
48-
actionBar.setDisplayHomeAsUpEnabled(true);
49-
actionBar.setDisplayShowHomeEnabled(true);
50-
//actionBar.setDisplayShowTitleEnabled(false);//隐藏Title
51-
actionBar.setTitle(title);
52-
}
53-
}
5453

5554
@Override
5655
protected void onDestroy() {
5756
super.onDestroy();
5857
BaseApplication.getIns().finishActivity(this);
5958
}
6059

60+
@Override
61+
public boolean onSupportNavigateUp() {
62+
onBackPressed();
63+
return true;
64+
}
65+
6166
}

module_news/src/main/java/com/guiying/news/main/NewsCenterActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ protected void onCreate(Bundle savedInstanceState) {
2828
super.onCreate(savedInstanceState);
2929
super.setContentView(R.layout.activity_news);
3030
mToolBar = (Toolbar) findViewById(R.id.news_title_bar);
31-
setToolbar(mToolBar, "知乎日报");
31+
mToolBar.setTitle("知乎日报");
32+
setupToolBar(mToolBar, false);
3233
mTabLayout = (TabLayout) findViewById(R.id.date_tab);
3334
mViewPager = (ViewPager) findViewById(R.id.message_pager);
3435
mListAdapter = new NewsListViewAdapter(getMessageListViews(), getWeekDate());

module_news/src/main/res/layout/activity_news.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6-
android:background="#ffff">
6+
android:background="#ffffff">
77

88
<android.support.design.widget.AppBarLayout
99
android:layout_width="match_parent"
10-
android:layout_height="wrap_content">
10+
android:layout_height="wrap_content"
11+
android:theme="@style/AppTheme.AppBarOverlay">
1112

1213
<android.support.v7.widget.Toolbar
1314
android:id="@+id/news_title_bar"
1415
android:layout_width="match_parent"
1516
android:layout_height="?attr/actionBarSize"
16-
android:background="?attr/colorPrimary"
17+
android:background="@color/colorPrimary"
18+
android:theme="@style/Toolbar"
1719
app:layout_scrollFlags="scroll|enterAlways" />
1820

1921
<android.support.design.widget.TabLayout
@@ -29,7 +31,7 @@
2931
android:id="@+id/message_pager"
3032
android:layout_width="match_parent"
3133
android:layout_height="wrap_content"
32-
android:background="#ffff"
34+
android:background="#ffffff"
3335
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
3436

3537

0 commit comments

Comments
 (0)