如何在Java项目中使用NavigationExtensions.kt? [英] How to use NavigationExtensions.kt in a Java project?

本文介绍了如何在Java项目中使用NavigationExtensions.kt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在新项目中使用BottomNavigation实现NavController.这是我的第一次尝试,到处都有很多关于此的模棱两可的信息.

I'm trying to implement NavController with BottomNavigation in a new project. This is my first attempt and there is a lot of ambiguous information all over the place about this.

因此,我的问题与每个底部"选项卡具有自己的后向堆栈并在底部导航水龙头之间保留片段有关.是的,我查看了 https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample ,由于该示例在Kotlin中,因此我无法理解如何将其与我现有的Java代码集成.

So my question relates to each Bottom tab having its own back stack and persisting the fragments between bottom navigation taps. Yes, I have looked at https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample and I'm unable to understand how to integrate this with my existing Java code as the sample is in Kotlin.

有人可以帮我吗?

推荐答案

好吧,经过数小时的尝试,我所做的是我使用了示例Kotlin项目,然后导入了我的Java类,资源和应用程序.该项目的资产.为了更改mainactivity.kt,我将其反编译并转到以下文件.这仍然是基本的工作原理,但是至少我已经有了框架,并有望节省一些人的时间...

Ok, after hours of trying, what I did was I used the sample Kotlin project and then imported my java classes, resources & assets to this project. In order to change the mainactivity.kt, I decompiled it and got to the following file. This is still basic workings, but at least I've got the framework and will hopefully save someone hours of head scratching...

package com.example.android.navigationadvancedsample;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.navigation.NavController;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.jetbrains.annotations.Nullable;

import java.util.List;

import kotlin.collections.CollectionsKt;


public class MainActivity extends AppCompatActivity {

private LiveData currentNavController;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    getSupportActionBar().hide();
    setContentView( R.layout.activity_main );
    if (savedInstanceState == null) {
        this.setupBottomNavigationBar();
    }
}

protected void onRestoreInstanceState(@Nullable Bundle savedInstanceState) {
    super.onRestoreInstanceState( savedInstanceState );
    this.setupBottomNavigationBar();
}


void setupBottomNavigationBar() {
    BottomNavigationView bottomNavigationView = this.findViewById( R.id.bottom_nav );
    List navGraphIds = CollectionsKt.listOf( new Integer[]{R.navigation.home, R.navigation.list, R.navigation.form} );
    LiveData controller = NavigationExtensionsKt.setupWithNavController( bottomNavigationView, navGraphIds, getSupportFragmentManager(), R.id.nav_host_container, getIntent() );
    controller.observe(  this, (Observer) (new Observer() {
        // $FF: synthetic method
        // $FF: bridge method
        public void onChanged(Object var1) {
            //this.onChanged((NavController)var1);
        }

    }) );

    this.currentNavController = controller;
}

public boolean onSupportNavigateUp() {
    LiveData var10000 = this.currentNavController;
    boolean var2;
    if (var10000 != null) {
        NavController var1 = (NavController)var10000.getValue();
        if (var1 != null) {
            var2 = var1.navigateUp();
            return var2;
        }
    }

    var2 = false;
    return var2;
}
}

这篇关于如何在Java项目中使用NavigationExtensions.kt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆