Skip to content

Commit 54efb4e

Browse files
authored
Merge pull request #428 from AppDevNext/ViewBinding
View binding
2 parents b244cfa + 35715a6 commit 54efb4e

8 files changed

Lines changed: 119 additions & 99 deletions

File tree

LogcatCoreUI/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ android {
1313
minSdkVersion 16
1414
project.archivesBaseName = "LogcatCore"
1515
}
16+
buildFeatures {
17+
viewBinding = true
18+
}
1619
namespace 'info.hannes.logcat.ui'
1720

1821
}

LogcatCoreUI/src/main/java/info/hannes/logcat/ui/BothLogActivity.kt

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,41 @@ package info.hannes.logcat.ui
22

33
import android.os.Bundle
44
import android.view.MenuItem
5-
import android.view.View
65
import androidx.appcompat.app.AppCompatActivity
6+
import info.hannes.logcat.ui.databinding.ActivityLogBinding
77

88
class BothLogActivity : AppCompatActivity() {
99

10+
private lateinit var binding: ActivityLogBinding
11+
1012
override fun onCreate(savedInstanceState: Bundle?) {
1113
super.onCreate(savedInstanceState)
12-
setContentView(R.layout.activity_log)
14+
binding = ActivityLogBinding.inflate(layoutInflater)
15+
setContentView(binding.root)
16+
1317
supportActionBar?.setDisplayHomeAsUpEnabled(true)
1418

15-
// Check that the activity is using the layout version with the fragment_container FrameLayout
16-
if (findViewById<View>(R.id.fragment_container) != null) {
17-
// However, if we're being restored from a previous state,
18-
// then we don't need to do anything and should return or else
19-
// we could end up with overlapping fragments.
20-
if (savedInstanceState != null) {
21-
return
22-
}
19+
// However, if we're being restored from a previous state,
20+
// then we don't need to do anything and should return or else
21+
// we could end up with overlapping fragments.
22+
if (savedInstanceState != null) {
23+
return
24+
}
2325

24-
// Create a new Fragment to be placed in the activity layout
25-
val bothLogFragment = BothLogsFragment.newInstance(
26-
"logfile.log",
27-
"search logfile",
28-
"search logcat",
29-
"your@mail.com"
30-
)
26+
// Create a new Fragment to be placed in the activity layout
27+
val bothLogFragment = BothLogsFragment.newInstance(
28+
"logfile.log",
29+
"search logfile",
30+
"search logcat",
31+
"your@mail.com"
32+
)
3133

32-
// In case this activity was started with special instructions from an
33-
// Intent, pass the Intent's extras to the fragment as arguments
34-
// firstFragment.arguments = intent.extras
34+
// In case this activity was started with special instructions from an
35+
// Intent, pass the Intent's extras to the fragment as arguments
36+
// firstFragment.arguments = intent.extras
3537

36-
// Add the fragment to the 'fragment_container' FrameLayout
37-
supportFragmentManager.beginTransaction().add(R.id.fragment_container, bothLogFragment).commit()
38-
}
38+
// Add the fragment to the 'fragment_container' FrameLayout
39+
supportFragmentManager.beginTransaction().add(R.id.fragment_container, bothLogFragment).commit()
3940
}
4041

4142
override fun onOptionsItemSelected(item: MenuItem): Boolean {

LogcatCoreUI/src/main/java/info/hannes/logcat/ui/LogcatActivity.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@ package info.hannes.logcat.ui
22

33
import android.os.Bundle
44
import android.view.MenuItem
5-
import android.view.View
65
import androidx.appcompat.app.AppCompatActivity
6+
import info.hannes.logcat.ui.databinding.ActivityLogBinding
77

88
class LogcatActivity : AppCompatActivity() {
99

10+
private lateinit var binding: ActivityLogBinding
11+
1012
override fun onCreate(savedInstanceState: Bundle?) {
1113
super.onCreate(savedInstanceState)
12-
setContentView(R.layout.activity_log)
13-
supportActionBar?.setDisplayHomeAsUpEnabled(true)
14+
binding = ActivityLogBinding.inflate(layoutInflater)
15+
setContentView(binding.root)
1416

15-
// Check that the activity is using the layout version with the fragment_container FrameLayout
16-
if (findViewById<View>(R.id.fragment_container) != null) {
17+
supportActionBar?.setDisplayHomeAsUpEnabled(true)
1718

18-
// However, if we're being restored from a previous state,
19-
// then we don't need to do anything and should return or else
20-
// we could end up with overlapping fragments.
21-
if (savedInstanceState != null) {
22-
return
23-
}
19+
// However, if we're being restored from a previous state,
20+
// then we don't need to do anything and should return or else
21+
// we could end up with overlapping fragments.
22+
if (savedInstanceState != null) {
23+
return
24+
}
2425

25-
// Create a new Fragment to be placed in the activity layout
26-
val firstFragment = LogcatFragment.newInstance("logcat.log", "search logcat")
26+
// Create a new Fragment to be placed in the activity layout
27+
val firstFragment = LogcatFragment.newInstance("logcat.log", "search logcat")
2728

28-
// In case this activity was started with special instructions from an
29-
// Intent, pass the Intent's extras to the fragment as arguments
30-
// firstFragment.arguments = intent.extras
29+
// In case this activity was started with special instructions from an
30+
// Intent, pass the Intent's extras to the fragment as arguments
31+
// firstFragment.arguments = intent.extras
3132

32-
// Add the fragment to the 'fragment_container' FrameLayout
33-
supportFragmentManager.beginTransaction().add(R.id.fragment_container, firstFragment).commit()
34-
}
33+
// Add the fragment to the 'fragment_container' FrameLayout
34+
supportFragmentManager.beginTransaction().add(R.id.fragment_container, firstFragment).commit()
3535
}
3636

3737
override fun onOptionsItemSelected(item: MenuItem): Boolean {

LogcatCoreUI/src/main/java/info/hannes/logcat/ui/LogfileActivity.kt

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,39 @@ package info.hannes.logcat.ui
22

33
import android.os.Bundle
44
import android.view.MenuItem
5-
import android.view.View
65
import androidx.appcompat.app.AppCompatActivity
6+
import info.hannes.logcat.ui.databinding.ActivityLogBinding
77

88
class LogfileActivity : AppCompatActivity() {
99

10+
private lateinit var binding: ActivityLogBinding
11+
1012
override fun onCreate(savedInstanceState: Bundle?) {
1113
super.onCreate(savedInstanceState)
12-
setContentView(R.layout.activity_log)
13-
supportActionBar?.setDisplayHomeAsUpEnabled(true)
14+
binding = ActivityLogBinding.inflate(layoutInflater)
15+
setContentView(binding.root)
1416

15-
// Check that the activity is using the layout version with the fragment_container FrameLayout
16-
if (findViewById<View>(R.id.fragment_container) != null) {
17-
18-
// However, if we're being restored from a previous state,
19-
// then we don't need to do anything and should return or else
20-
// we could end up with overlapping fragments.
21-
if (savedInstanceState != null) {
22-
return
23-
}
17+
supportActionBar?.setDisplayHomeAsUpEnabled(true)
2418

25-
// Create a new Fragment to be placed in the activity layout
26-
val firstFragment = LogfileFragment.newInstance(
27-
"logfile.log",
28-
"search logfile"
29-
)
19+
// However, if we're being restored from a previous state,
20+
// then we don't need to do anything and should return or else
21+
// we could end up with overlapping fragments.
22+
if (savedInstanceState != null) {
23+
return
24+
}
3025

31-
// In case this activity was started with special instructions from an
32-
// Intent, pass the Intent's extras to the fragment as arguments
33-
// firstFragment.arguments = intent.extras
26+
// Create a new Fragment to be placed in the activity layout
27+
val firstFragment = LogfileFragment.newInstance(
28+
"logfile.log",
29+
"search logfile"
30+
)
3431

35-
// Add the fragment to the 'fragment_container' FrameLayout
36-
supportFragmentManager.beginTransaction().add(R.id.fragment_container, firstFragment).commit()
37-
}
32+
// In case this activity was started with special instructions from an
33+
// Intent, pass the Intent's extras to the fragment as arguments
34+
// firstFragment.arguments = intent.extras
3835

36+
// Add the fragment to the 'fragment_container' FrameLayout
37+
supportFragmentManager.beginTransaction().add(R.id.fragment_container, firstFragment).commit()
3938
}
4039

4140
override fun onOptionsItemSelected(item: MenuItem): Boolean {

sample/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ android {
1919
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2020
}
2121

22+
buildFeatures {
23+
viewBinding = true
24+
}
25+
2226
buildTypes {
2327
release {
2428
}

sample/src/main/java/info/hannes/logcat/sample/CrashlyticApplication.kt

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package info.hannes.logcat.sample
22

33
import android.annotation.SuppressLint
4-
import android.os.Handler
5-
import android.os.Looper
6-
import android.os.StrictMode
4+
import android.app.Activity
5+
import android.os.*
76
import android.provider.Settings
87
import com.google.firebase.crashlytics.FirebaseCrashlytics
98
import info.hannes.crashlytic.CrashlyticsTree
@@ -55,5 +54,35 @@ class CrashlyticApplication : LoggingApplication() {
5554
}
5655

5756
Handler(Looper.getMainLooper()).postDelayed(runner, 3000)
57+
58+
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
59+
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
60+
Timber.v("${activity.javaClass.simpleName} onCreate(Bundle) starting")
61+
}
62+
63+
override fun onActivityStarted(activity: Activity) {
64+
Timber.v("${activity.javaClass.simpleName} onStart() starting")
65+
}
66+
67+
override fun onActivityResumed(activity: Activity) {
68+
Timber.v("${activity.javaClass.simpleName} onResume() starting")
69+
}
70+
71+
override fun onActivityPaused(activity: Activity) {
72+
Timber.v("${activity.javaClass.simpleName} onPause() ending")
73+
}
74+
75+
override fun onActivityStopped(activity: Activity) {
76+
Timber.v("${activity.javaClass.simpleName} onStop() ending")
77+
}
78+
79+
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
80+
Timber.v("${activity.javaClass.simpleName} onSaveInstanceState(Bundle) starting")
81+
}
82+
83+
override fun onActivityDestroyed(activity: Activity) {
84+
Timber.v("${activity.javaClass.simpleName} onDestroy() ending")
85+
}
86+
})
5887
}
5988
}

sample/src/main/java/info/hannes/logcat/sample/MainActivity.kt

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,47 @@ import android.os.Bundle
66
import android.view.Menu
77
import android.view.MenuItem
88
import androidx.appcompat.app.AppCompatActivity
9-
import androidx.appcompat.widget.Toolbar
109
import androidx.core.view.GravityCompat
11-
import androidx.drawerlayout.widget.DrawerLayout
1210
import com.google.android.material.navigation.NavigationView
11+
import info.hannes.logcat.sample.databinding.ActivityMainBinding
1312
import info.hannes.logcat.ui.BothLogActivity
1413
import info.hannes.logcat.ui.LogcatActivity
1514
import info.hannes.logcat.ui.LogfileActivity
1615

1716
class MainActivity : AppCompatActivity() {
1817

19-
private lateinit var drawerLayout: DrawerLayout
18+
private lateinit var binding: ActivityMainBinding
2019

2120
override fun onCreate(savedInstanceState: Bundle?) {
2221
super.onCreate(savedInstanceState)
23-
setContentView(R.layout.activity_main)
22+
binding = ActivityMainBinding.inflate(layoutInflater)
23+
setContentView(binding.root)
2424

25-
drawerLayout = findViewById(R.id.drawer_layout)
26-
27-
val toolbar = findViewById<Toolbar>(R.id.toolbar)
28-
setSupportActionBar(toolbar)
25+
setSupportActionBar(binding.containMain.toolbar)
2926

3027
// enable ActionBar app icon to behave as action to toggle nav drawer
3128
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_menu)
3229
supportActionBar?.setDisplayHomeAsUpEnabled(true)
3330

34-
val navigationView = findViewById<NavigationView>(R.id.nav_view)
35-
if (navigationView != null) {
36-
setupDrawerContent(navigationView)
37-
}
38-
31+
setupDrawerContent(binding.navView)
3932
}
4033

4134
private fun setupDrawerContent(navigationView: NavigationView) {
4235
navigationView.setNavigationItemSelectedListener { menuItem ->
4336
menuItem.isChecked = true
4437
selectNavigationItem(menuItem.itemId)
45-
drawerLayout.closeDrawers()
38+
binding.drawerLayout.closeDrawers()
4639
true
4740
}
48-
4941
}
5042

5143
private fun selectNavigationItem(itemId: Int) {
52-
5344
when (itemId) {
5445
R.id.nav_drawer_logcat -> startActivity(Intent(this, LogcatActivity::class.java))
5546
R.id.nav_drawer_logfile -> startActivity(Intent(this, LogfileActivity::class.java))
5647
R.id.nav_drawer_both_logfiles -> startActivity(Intent(this, BothLogActivity::class.java))
5748
R.id.nav_drawer_other_github -> {
58-
val url = "https://github.com/hannesa2/Logcat"
49+
val url = "https://github.com/AppDevNext/Logcat"
5950
val i = Intent(Intent.ACTION_VIEW)
6051
i.data = Uri.parse(url)
6152
startActivity(i)
@@ -70,20 +61,11 @@ class MainActivity : AppCompatActivity() {
7061

7162
override fun onOptionsItemSelected(item: MenuItem): Boolean {
7263
when (item.itemId) {
73-
android.R.id.home -> {
74-
drawerLayout.openDrawer(GravityCompat.START)
75-
}
76-
R.id.nav_drawer_logcat -> {
77-
startActivity(Intent(this, LogcatActivity::class.java))
78-
}
79-
R.id.nav_drawer_logfile -> {
80-
startActivity(Intent(this, LogfileActivity::class.java))
81-
}
82-
R.id.nav_drawer_both_logfiles -> {
83-
startActivity(Intent(this, BothLogActivity::class.java))
84-
}
64+
android.R.id.home -> binding.drawerLayout.openDrawer(GravityCompat.START)
65+
R.id.nav_drawer_logcat -> startActivity(Intent(this, LogcatActivity::class.java))
66+
R.id.nav_drawer_logfile -> startActivity(Intent(this, LogfileActivity::class.java))
67+
R.id.nav_drawer_both_logfiles -> startActivity(Intent(this, BothLogActivity::class.java))
8568
}
86-
8769
return true
8870
}
8971

sample/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
android:layout_height="match_parent"
77
android:fitsSystemWindows="true">
88

9-
<include layout="@layout/content_main" />
9+
<include
10+
android:id="@+id/contain_main"
11+
layout="@layout/content_main" />
1012

1113
<com.google.android.material.navigation.NavigationView
1214
android:id="@+id/nav_view"

0 commit comments

Comments
 (0)