Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Kotlin Anonymous Inner Class

Posted on November 11, 2022 By

Solution 1 :

Functionality wise, both the snippets are the same.

Snippet 1 is using a lambda expression for setOnItemSelectedListener whereas the Snippet 2 uses a singleton object implementing the interface ChipNavigationBar.OnItemSelectedListener.

To simply your code, you should use a lambda expression.
Also, you can use lambda expression only if your interface has only 1 function that’s need to overridden.

Problem :

I have the following code snippets
Please help me figure out the difference between the same

Snippet 1

    navbar.setOnItemSelectedListener { id ->
        var fragment: Fragment? = null
        when (id) {
            R.id.home -> fragment = HomeFragment()
            R.id.graphical_stats -> fragment = GraphicalStats()
            R.id.sources -> fragment = SourcesFragment()
        }
        if (fragment != null) {
            supportFragmentManager.beginTransaction().replace(R.id.container_frame, fragment)
                .commit()
        } else {
            Log.e(TAG, "Error Creating Fragment")
        }
    }

Snippet 2

navbar.setOnItemSelectedListener { object : ChipNavigationBar.OnItemSelectedListener{
        override fun onItemSelected(id: Int) {
            var fragment: Fragment? = null
            when (id) {
                R.id.home -> fragment = HomeFragment()
                R.id.graphical_stats -> fragment = GraphicalStats()
                R.id.sources -> fragment = SourcesFragment()
            }
            if (fragment != null) {
                supportFragmentManager.beginTransaction().replace(R.id.container_frame, fragment)
                    .commit()
            } else {
                Log.e(TAG, "Error Creating Fragment")
            }
        }

I am using ChipNavigationBar and have three fragments namely Home Graphical Stats and Sources which will be created or swapped accordingly

Comments

Comment posted by gtxtreme

Actually I tried the second one but it doesn’t seem to work Only the first one worked any idea why?

Comment posted by Alpha 1

Because you have added { before setOnItemSelectedListener it should be a normal bracket ( . it should be like this :

READ  [FIXED] android - null pointer exception of nested Parcelable in intent getextra
Powered by Inline Related Posts
Android Tags:android, kotlin

Post navigation

Previous Post: [FIXED] android – Can you get the tag of a date picker
Next Post: [FIXED] Android Recyclerview add item and notifyiteminserted causes blink recycled view before draw the correct

Related Posts

[FIXED] javascript – I want to Scroll down android app page but scroll only works once Android
[FIXED] java – how to change scroll vertical to horizontal when orientation change? Android
[FIXED] java – Settings Back Button Isn’t Working Using Bellow Code? Android
[FIXED] android – How to do ListView correct refresh? Android
[FIXED] android – How to import a variable from another class in java Android
[FIXED] android – I have upload my app into play store, but some devices not appeared Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme