Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep BaseActivity in Java compatible with Kotlin Activity

I would like to know if there is a way to maintain a BaseActivity (Java) that has, for example, functional BottomNavigationView for Java and Kotlin Activities. In the future I intend to turn the (several) Java Activities into Kotlin, since the new Activities are developed in Kotlin. The problem is that Java is able to keep BottomNavigationView created correctly since Kotlin plays as null and BottomNavigationView loading comes after the loop maintained as in Java. Another attempt was to try to turn my Java BaseActivity into Kotlin but broke the operation of the various Java Activities.

class KotlinActivity : BaseActivity() {

    public override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my_wishes_list)
        ButterKnife.bind(this)
        //bottomNavigationMenu is null and lost all configurations from the BaseActivity
        bottomNavigationMenu.menu.getItem(0).isChecked = true

    }

}


public class BaseActivity extends AppCompatActivity {

    @BindView(R.id.bottom_navigation_menu)
    protected BottomNavigationView bottomNavigationMenu;

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        setupBottomMenu();
    }

    private void setupBottomMenu() {

        if (bottomNavigationMenu != null) {

            BottomNavigationViewHelper.removeShiftMode(bottomNavigationMenu);

            bottomNavigationMenu.setOnNavigationItemSelectedListener(
                    new BottomNavigationView.OnNavigationItemSelectedListener() {
                        @Override
                        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                            setupBottomMenuListener(item);
                            return true;
                        }

                    });


        }

    }

    private void setupBottomMenuListener(@NonNull MenuItem item) {

        switch (item.getItemId()) {

           //...

        }

    }

}
like image 482
user6505882 Avatar asked Jun 06 '26 15:06

user6505882


1 Answers

Why don't you simply use Kotlin Android Binding? Which is an Extension from Kotlin helps to bind the view without any "bindView" or findViewById code to interfere business logic.

have a look. https://kotlinlang.org/docs/tutorials/android-plugin.html

like image 101
dwqdq13213 Avatar answered Jun 09 '26 04:06

dwqdq13213



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!