How to use NavigationExtensions.kt in a Java project?

萝らか妹 提交于 2020-08-08 19:03:42

问题


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.

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.

Can anyone help me with this?


回答1:


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;
}
}



回答2:


Another Solution

Simply save NavigationExtensions.kt as it is in java project and use it in java code.

Make a class NavigationExtensions with the content of NavigationExtensions.kt file.

Need to enable project for Kotlin as well. Here are the changes I made to my App build.gridle -

  1. Apply plugin apply plugin: 'kotlin-android' and apply plugin: 'kotlin-android-extensions'

  2. Implement Kotlin extension in my dependencies implementation 'androidx.core:core-ktx:1.3.1'

  3. Added Kotlin jvmTarget 1.8 as

    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.2"
        defaultConfig {
        .........
        }
    
        compileOptions {
            sourceCompatibility 1.8
            targetCompatibility 1.8
        }
    
    
        kotlinOptions {
            jvmTarget = "1.8"
        }
    
        buildTypes {
            release {
                .......
            }
        }
    }
    

This is how to use the NavigationExtensions in MainActivity.java

public class MainActivity extends AppCompatActivity {

    private LiveData<NavController> currentNavController = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_view);

        List<Integer> navGraphList = new ArrayList<>();
        navGraphList.add(R.navigation.dashboard_navigation);
        navGraphList.add(R.navigation.find_navigation);
        navGraphList.add(R.navigation.options_navigation);


        LiveData<NavController> navControllerLiveData = new NavigationExtensions().setupWithNavController(
                bottomNavigationView
                , navGraphList
                , getSupportFragmentManager()
                , R.id.fragment_container, getIntent()
        );

        currentNavController = navControllerLiveData;
    }

    @Override
    public boolean onNavigateUp() {
        currentNavController.getValue().navigateUp();
        return super.onNavigateUp();
    }
}

NavigationExtension file location in project .

Edit (Fix Configuration Changes)

To survive a configuration change (like screen rotation), you need to following changes under MainActivity.java

public class MainActivity extends AppCompatActivity {

    private LiveData<NavController> currentNavController = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(savedInstanceState==null){
            setupBottomNavigation();
        }
        //else we need to wait for onRestoreInstanceState
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        setupBottomNavigation();
    }

    @Override
    public boolean onNavigateUp() {
        currentNavController.getValue().navigateUp();
        return super.onNavigateUp();
    }

    private void setupBottomNavigation(){
        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_view);

        List<Integer> navGraphList = new ArrayList<>();
        navGraphList.add(R.navigation.dashboard_navigation);
        navGraphList.add(R.navigation.find_navigation);
        navGraphList.add(R.navigation.options_navigation);


        LiveData<NavController> navControllerLiveData = new NavigationExtensions().setupWithNavController(
                bottomNavigationView
                , navGraphList
                , getSupportFragmentManager()
                , R.id.fragment_container, getIntent()
        );
        currentNavController = navControllerLiveData;
    }
}

Happy Coding !



来源:https://stackoverflow.com/questions/59251349/how-to-use-navigationextensions-kt-in-a-java-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!