I have been trying to set my default selected tab using the widely accepted java method in my onCreate (bottomNavigationView.setSelectedItemId(R.id.item_id)) but this does not seem to work in Kotlin. Also, the fragments that are set to load on each selected item seem to load every time that menu item is selected, as well as drawing their contents over the previous fragment.
How can I set the default selected tab for my BottomNavigationView? Also am I correctly loading fragments or is my code not properly disposing of fragments before loading another one?
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val SELECTED_ITEM = "arg_selected_item"
private var mBottomNav: BottomNavigationView? = null
private var mSelectedItem: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mBottomNav = findViewById(R.id.navigation) as BottomNavigationView
mBottomNav!!.setOnNavigationItemSelectedListener { item ->
selectFragment(item)
true
}
val selectedItem: MenuItem
if (savedInstanceState != null) {
mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0)
selectedItem = mBottomNav!!.menu.findItem(mSelectedItem)
} else {
selectedItem = mBottomNav!!.menu.getItem(0)
}
selectFragment(selectedItem)
}
override fun onSaveInstanceState(outState: Bundle) {
outState.putInt(SELECTED_ITEM, mSelectedItem)
super.onSaveInstanceState(outState)
}
override fun onBackPressed() {
val homeItem = mBottomNav!!.menu.getItem(0)
if (mSelectedItem != homeItem.itemId) {
// select home item
selectFragment(homeItem)
} else {
super.onBackPressed()
}
}
private fun selectFragment(item: MenuItem) {
var frag: Fragment? = null
// init corresponding fragment
when (item.itemId) {
R.id.navigation_enrollments -> frag = EnrollmentsFragment.newInstance()
R.id.navigation_timeline -> frag = TimelineFragment.newInstance()
R.id.navigation_home -> frag = HomeFragment.newInstance()
R.id.navigation_alerts -> frag = AlertsFragment.newInstance()
R.id.navigation_profile -> frag = ProfileFragment.newInstance()
}
// update selected item
mSelectedItem = item.itemId
// uncheck the other items.
for (i in 0..mBottomNav!!.menu.size() - 1) {
val menuItem = mBottomNav!!.menu.getItem(i)
menuItem.isChecked = menuItem.itemId == item.itemId
}
updateToolbarText(item.title)
if (frag != null) {
val ft = supportFragmentManager.beginTransaction()
ft.add(R.id.content, frag, frag.tag)
ft.commit()
}
}
private fun updateToolbarText(text: CharSequence) {
val actionBar = supportActionBar
if (actionBar != null) {
actionBar.title = text
}
}
private fun getColorFromRes(#ColorRes resId: Int): Int {
return ContextCompat.getColor(this, resId)
}
}
Resolved the default selected tab issue by adding the following code to my onCreate function:
val bottomNavigationView: BottomNavigationView = findViewById(R.id.navigation) as BottomNavigationView
bottomNavigationView.selectedItemId = R.id.navigation_home
Also, I resolved the issue of fragments being drawn on top of one another by modifying the following:
if (frag != null) {
val ft = supportFragmentManager.beginTransaction()
ft.replace(R.id.content, frag, frag.tag)
ft.commit()
}
}
I changed ft.add to ft.replace
Related
I am trying to go to another activity, but my setting activity is preventing to go there. How do I fix that?
class MainActivity : AppCompatActivity() {
private var gettext: EditText? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
gettext = findViewById(R.id.text2)
// val intent = intent
val action = intent.action
val type = intent.type
val button = findViewById<Button>(R.id.button_submit)
//this part is what I want to go to another activity, while still have setting activity
button.setOnClickListener {
val intent = Intent(this,com.example.testing1.DecryptVerify::class.java)
startActivity(intent)
}
if (Intent.ACTION_SEND == action && type != null) {
handleSendText(intent)
}
}
private fun handleSendText(intent: Intent) {
val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT)
if (sharedText != null) {
// showing the text in edittext
gettext?.setText(sharedText)
}
}
//I want to stay, while when I run it and click button on the top one
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_settings -> {
val intent = Intent(this, com.example.testing1.SettingsActivity::class.java
)
startActivity(intent)
}
}
return super.onOptionsItemSelected(item)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val menuInflater = menuInflater
menuInflater.inflate(R.menu.main_menu, menu)
return super.onCreateOptionsMenu(menu)
}
}
Do I need to some form if-else?
I want to use my own back icon in toolbar while using navigation component and bottom nav menu.
I have tried every possible solution but still I am displayed default back icon.
When I write
binding.toolbar.setNavigationIcon(R.drawable.ic_arrow_back)
in onCreate MainActivity then that icon is displayed but when I navigate to next screen it uses default.
I added onDestinationListener
val destinationChangedListener =
NavController.OnDestinationChangedListener { controller, destination, arguments ->
when(destination.id){
R.id.subCategoriesFragment -> {
// Nothing is working
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_back)
binding.toolbar.setNavigationIcon(R.drawable.ic_arrow_back)
}
else -> {
binding.toolbar.navigationIcon = null
}
}
}
Still toolbar displays default icon.
I am using google's Navigation extention which manages backstacks of all bottom menus.
Extension code is below.
/**
* Manages the various graphs needed for a [BottomNavigationView].
*
* This sample is a workaround until the Navigation Component supports multiple back stacks.
*/
fun BottomNavigationView.setupWithNavController(
navGraphIds: List<Int>,
fragmentManager: FragmentManager,
containerId: Int,
intent: Intent
): LiveData<NavController> {
// Map of tags
val graphIdToTagMap = SparseArray<String>()
// Result. Mutable live data with the selected controlled
val selectedNavController = MutableLiveData<NavController>()
var firstFragmentGraphId = 0
// First create a NavHostFragment for each NavGraph ID
navGraphIds.forEachIndexed { index, navGraphId ->
val fragmentTag = getFragmentTag(index)
// Find or create the Navigation host fragment
val navHostFragment = obtainNavHostFragment(
fragmentManager,
fragmentTag,
navGraphId,
containerId
)
// Obtain its id
val graphId = navHostFragment.navController.graph.id
if (index == 0) {
firstFragmentGraphId = graphId
}
// Save to the map
graphIdToTagMap[graphId] = fragmentTag
// Attach or detach nav host fragment depending on whether it's the selected item.
if (this.selectedItemId == graphId) {
// Update livedata with the selected graph
selectedNavController.value = navHostFragment.navController
attachNavHostFragment(fragmentManager, navHostFragment, index == 0)
} else {
detachNavHostFragment(fragmentManager, navHostFragment)
}
}
// Now connect selecting an item with swapping Fragments
var selectedItemTag = graphIdToTagMap[this.selectedItemId]
val firstFragmentTag = graphIdToTagMap[firstFragmentGraphId]
var isOnFirstFragment = selectedItemTag == firstFragmentTag
// When a navigation item is selected
setOnNavigationItemSelectedListener { item ->
LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(SELECTED_RESELETED_ITEM).apply {
putExtra("item", item.itemId)
})
// Don't do anything if the state is state has already been saved.
if (fragmentManager.isStateSaved) {
false
} else {
val newlySelectedItemTag = graphIdToTagMap[item.itemId]
if (selectedItemTag != newlySelectedItemTag) {
// Pop everything above the first fragment (the "fixed start destination")
fragmentManager.popBackStack(firstFragmentTag,
FragmentManager.POP_BACK_STACK_INCLUSIVE)
val selectedFragment = fragmentManager.findFragmentByTag(newlySelectedItemTag)
as NavHostFragment
// Exclude the first fragment tag because it's always in the back stack.
if (firstFragmentTag != newlySelectedItemTag) {
// Commit a transaction that cleans the back stack and adds the first fragment
// to it, creating the fixed started destination.
fragmentManager.beginTransaction()
.setCustomAnimations(
R.anim.nav_default_enter_anim,
R.anim.nav_default_exit_anim,
R.anim.nav_default_pop_enter_anim,
R.anim.nav_default_pop_exit_anim)
.attach(selectedFragment)
.setPrimaryNavigationFragment(selectedFragment)
.apply {
// Detach all other Fragments
graphIdToTagMap.forEach { _, fragmentTagIter ->
if (fragmentTagIter != newlySelectedItemTag) {
detach(fragmentManager.findFragmentByTag(firstFragmentTag)!!)
}
}
}
.addToBackStack(firstFragmentTag)
.setReorderingAllowed(true)
.commit()
}
selectedItemTag = newlySelectedItemTag
isOnFirstFragment = selectedItemTag == firstFragmentTag
selectedNavController.value = selectedFragment.navController
true
} else {
false
}
}
}
// Optional: on item reselected, pop back stack to the destination of the graph
setupItemReselected(graphIdToTagMap, fragmentManager)
// Handle deep link
setupDeepLinks(navGraphIds, fragmentManager, containerId, intent)
// Finally, ensure that we update our BottomNavigationView when the back stack changes
fragmentManager.addOnBackStackChangedListener {
if (!isOnFirstFragment && !fragmentManager.isOnBackStack(firstFragmentTag)) {
this.selectedItemId = firstFragmentGraphId
}
// Reset the graph if the currentDestination is not valid (happens when the back
// stack is popped after using the back button).
selectedNavController.value?.let { controller ->
if (controller.currentDestination == null) {
controller.navigate(controller.graph.id)
}
}
}
return selectedNavController
}
private fun BottomNavigationView.setupDeepLinks(
navGraphIds: List<Int>,
fragmentManager: FragmentManager,
containerId: Int,
intent: Intent
) {
navGraphIds.forEachIndexed { index, navGraphId ->
val fragmentTag = getFragmentTag(index)
// Find or create the Navigation host fragment
val navHostFragment = obtainNavHostFragment(
fragmentManager,
fragmentTag,
navGraphId,
containerId
)
// Handle Intent
if (navHostFragment.navController.handleDeepLink(intent)
&& selectedItemId != navHostFragment.navController.graph.id) {
this.selectedItemId = navHostFragment.navController.graph.id
}
}
}
private fun BottomNavigationView.setupItemReselected(
graphIdToTagMap: SparseArray<String>,
fragmentManager: FragmentManager
) {
setOnNavigationItemReselectedListener { item ->
LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(SELECTED_RESELETED_ITEM).apply {
putExtra("item", item.itemId)
})
val newlySelectedItemTag = graphIdToTagMap[item.itemId]
val selectedFragment = fragmentManager.findFragmentByTag(newlySelectedItemTag)
as NavHostFragment
val navController = selectedFragment.navController
// Pop the back stack to the start destination of the current navController graph
navController.popBackStack(
navController.graph.startDestination, false
)
}
}
private fun detachNavHostFragment(
fragmentManager: FragmentManager,
navHostFragment: NavHostFragment
) {
fragmentManager.beginTransaction()
.detach(navHostFragment)
.commitNow()
}
private fun attachNavHostFragment(
fragmentManager: FragmentManager,
navHostFragment: NavHostFragment,
isPrimaryNavFragment: Boolean
) {
fragmentManager.beginTransaction()
.attach(navHostFragment)
.apply {
if (isPrimaryNavFragment) {
setPrimaryNavigationFragment(navHostFragment)
}
}
.commitNow()
}
private fun obtainNavHostFragment(
fragmentManager: FragmentManager,
fragmentTag: String,
navGraphId: Int,
containerId: Int
): NavHostFragment {
// If the Nav Host fragment exists, return it
val existingFragment = fragmentManager.findFragmentByTag(fragmentTag) as NavHostFragment?
existingFragment?.let { return it }
// Otherwise, create it and return it.
val navHostFragment = NavHostFragment.create(navGraphId)
fragmentManager.beginTransaction()
.add(containerId, navHostFragment, fragmentTag)
.commitNow()
return navHostFragment
}
private fun FragmentManager.isOnBackStack(backStackName: String): Boolean {
val backStackCount = backStackEntryCount
for (index in 0 until backStackCount) {
if (getBackStackEntryAt(index).name == backStackName) {
return true
}
}
return false
}
private fun getFragmentTag(index: Int) = "bottomNavigation#$index"
Can anyone please help, how I can use my own back icon.
The developers of the navigation component didn't provide a way to change the icon.
But this is possible by next way:
Create a package in your project: "androidx.navigation.ui"
Create ModifiedAbstractAppBarOnDestinationChangedListener:
package androidx.navigation.ui;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.graphics.drawable.DrawerArrowDrawable;
import androidx.customview.widget.Openable;
import androidx.navigation.FloatingWindow;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import java.lang.ref.WeakReference;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* The abstract OnDestinationChangedListener for keeping any type of app bar updated.
* This handles both updating the title and updating the Up Indicator, transitioning between
* the drawer icon and up arrow as needed.
* #hide
*/
public abstract class ModifiedAbstractAppBarOnDestinationChangedListener
implements NavController.OnDestinationChangedListener {
private final Context mContext;
private final Set<Integer> mTopLevelDestinations;
#Nullable
private final WeakReference<Openable> mOpenableLayoutWeakReference;
private DrawerArrowDrawable mArrowDrawable;
private ValueAnimator mAnimator;
ModifiedAbstractAppBarOnDestinationChangedListener(#NonNull Context context,
#NonNull AppBarConfiguration configuration) {
mContext = context;
mTopLevelDestinations = configuration.getTopLevelDestinations();
Openable openableLayout = configuration.getOpenableLayout();
if (openableLayout != null) {
mOpenableLayoutWeakReference = new WeakReference<>(openableLayout);
} else {
mOpenableLayoutWeakReference = null;
}
}
protected abstract void setTitle(CharSequence title);
protected abstract void setNavigationIcon(Drawable icon, #StringRes int contentDescription, int destinationId);
#Override
public void onDestinationChanged(#NonNull NavController controller,
#NonNull NavDestination destination, #Nullable Bundle arguments) {
if (destination instanceof FloatingWindow) {
return;
}
Openable openableLayout = mOpenableLayoutWeakReference != null
? mOpenableLayoutWeakReference.get()
: null;
if (mOpenableLayoutWeakReference != null && openableLayout == null) {
controller.removeOnDestinationChangedListener(this);
return;
}
int id = destination.getId();
CharSequence label = destination.getLabel();
if (label != null) {
// Fill in the data pattern with the args to build a valid URI
StringBuffer title = new StringBuffer();
Pattern fillInPattern = Pattern.compile("\\{(.+?)\\}");
Matcher matcher = fillInPattern.matcher(label);
while (matcher.find()) {
String argName = matcher.group(1);
if (arguments != null && arguments.containsKey(argName)) {
matcher.appendReplacement(title, "");
//noinspection ConstantConditions
title.append(arguments.get(argName).toString());
} else {
throw new IllegalArgumentException("Could not find " + argName + " in "
+ arguments + " to fill label " + label);
}
}
matcher.appendTail(title);
setTitle(title);
}
boolean isTopLevelDestination = NavigationUI.matchDestinations(destination,
mTopLevelDestinations);
if (openableLayout == null && isTopLevelDestination) {
setNavigationIcon(null, 0, id);
} else {
setActionBarUpIndicator(openableLayout != null && isTopLevelDestination, id);
}
}
private void setActionBarUpIndicator(boolean showAsDrawerIndicator, int destinationId) {
boolean animate = true;
if (mArrowDrawable == null) {
mArrowDrawable = new DrawerArrowDrawable(mContext);
// We're setting the initial state, so skip the animation
animate = false;
}
setNavigationIcon(mArrowDrawable, showAsDrawerIndicator
? R.string.nav_app_bar_open_drawer_description
: R.string.nav_app_bar_navigate_up_description,
destinationId);
float endValue = showAsDrawerIndicator ? 0f : 1f;
if (animate) {
float startValue = mArrowDrawable.getProgress();
if (mAnimator != null) {
mAnimator.cancel();
}
mAnimator = ObjectAnimator.ofFloat(mArrowDrawable, "progress",
startValue, endValue);
mAnimator.start();
} else {
mArrowDrawable.setProgress(endValue);
}
}
}
Create ModifiedToolbarOnDestinationChangedListener and add ability to change or replace the drawable icon with getModifiedIcon(drawable, destinationId) method:
package androidx.navigation.ui;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.Toolbar;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.transition.TransitionManager;
import java.lang.ref.WeakReference;
/**
* The OnDestinationChangedListener specifically for keeping a Toolbar updated.
* This handles both updating the title and updating the Up Indicator, transitioning between
* the drawer icon and up arrow as needed.
* #hide
*/
public class ModifiedToolbarOnDestinationChangedListener extends
ModifiedAbstractAppBarOnDestinationChangedListener {
private final WeakReference<Toolbar> mToolbarWeakReference;
public ModifiedToolbarOnDestinationChangedListener(
#NonNull Toolbar toolbar, #NonNull AppBarConfiguration configuration) {
super(toolbar.getContext(), configuration);
mToolbarWeakReference = new WeakReference<>(toolbar);
}
#Override
public void onDestinationChanged(#NonNull NavController controller,
#NonNull NavDestination destination, #Nullable Bundle arguments) {
Toolbar toolbar = mToolbarWeakReference.get();
if (toolbar == null) {
controller.removeOnDestinationChangedListener(this);
return;
}
super.onDestinationChanged(controller, destination, arguments);
}
#Override
protected void setTitle(CharSequence title) {
mToolbarWeakReference.get().setTitle(title);
}
#Override
protected void setNavigationIcon(Drawable icon, #StringRes int contentDescription, int destinationId) {
Toolbar toolbar = mToolbarWeakReference.get();
if (toolbar != null) {
Drawable modifiedIcon = getModifiedIcon(icon, destinationId);
boolean useTransition = modifiedIcon == null && toolbar.getNavigationIcon() != null;
toolbar.setNavigationIcon(modifiedIcon);
toolbar.setNavigationContentDescription(contentDescription);
if (useTransition) {
TransitionManager.beginDelayedTransition(toolbar);
}
}
}
#Nullable
protected Drawable getModifiedIcon(#Nullable Drawable drawable, int destinationId) {
return drawable;
}
}
Setup the Navigation component with modified classes and change the drawable icon in depend on destinationId:
fun setupWithNavController(toolbar: Toolbar, navController: NavController, configuration: AppBarConfiguration) {
val listener = object : ModifiedToolbarOnDestinationChangedListener(toolbar, configuration) {
override fun getModifiedIcon(drawable: Drawable?, destinationId: Int): Drawable? {
// Return the drawable in depend on destinationId
if (drawable is DrawerArrowDrawable){
drawable.color = Icon.getInstance().iconsColor
}
return drawable
}
}
navController.addOnDestinationChangedListener(listener)
toolbar.setNavigationOnClickListener { NavigationUI.navigateUp(navController, configuration) }
}
I want to switch Fragments by button click inside the fragment itself. I have have created viewpager + tablayout, everything worked until i made an interface to communicate with my activity where i can set different Page for Viewpager. I am pretty new to Kotlin and I do not understand why I am catching NullPointer :(
I probably initialized my button poorly too, but I am not sure
Here is my MainActivity code
class MainActivity : AppCompatActivity()
, MyFragment1.buttonClick {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tablayout.addTab(tablayout.newTab().setIcon(R.drawable.ic_fragment1))
tablayout.addTab(tablayout.newTab().setIcon(R.drawable.ic_fragment_2))
tablayout.addTab(tablayout.newTab().setIcon(R.drawable.ic_fragment3))
tablayout.tabGravity = TabLayout.GRAVITY_FILL
val adapter = MyAdapter(this, supportFragmentManager, tablayout.tabCount)
viewpager!!.adapter = adapter
viewpager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tablayout))
val tabStrip = tablayout.getChildAt(0) as LinearLayout
for (i in 0 until tabStrip.childCount) {
tabStrip.getChildAt(i).setOnTouchListener { v, event -> true }
}
}
override fun buttonClicked(view: View) {
viewpager.currentItem = 2
}
fun selectIndex(index: Int) {
viewpager.currentItem = index
}
override fun onBackPressed() {
val currentPos = viewpager.currentItem
if (currentPos != 0) {
viewpager.currentItem = 0
} else {
super.onBackPressed()
}
}
}
**And a code for my Fragment**
class MyFragment1 : Fragment() {
private var click: buttonClick? = null
interface buttonClick {
fun buttonClicked(view: View)
}
var currentPage: Int = 0
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_my_fragment1, container, false)
val btn: Button = view?.findViewById(R.id.btn_next)
btn.setOnClickListener {
Toast.makeText(context, "test", Toast.LENGTH_LONG).show()
click?.buttonClicked(it)
}
return view
}
}
Have you initialized your click variable?
private var click: buttonClick? = null
override fun onAttach(context: Context?) {
super.onAttach(context)
click = context as? buttonClick
}
override fun onDetach() {
super.onDetach()
click = null
}
I have a bottom navigation which runs on my Main Activity
private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
LoadFragment(e.Item.ItemId);
}
void LoadFragment(int id)
{
Android.Support.V4.App.Fragment fragment = null;
switch (id)
{
case Resource.Id.navigation_1:
fragment = Fragment1.NewInstance();
break;
case Resource.Id.navigation_2:
fragment = Fragment2.NewInstance();
break;
case Resource.Id.navigation_3:
fragment = Fragment3.NewInstance();
break;
}
if (fragment == null)
return;
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
}
In my Fragment1 (which is Resource.Id.navigation_1) I am refreshing data
public async override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
RefreshItems(false);
}
The issue is, every time I click on navigation_1 for fragment1, the data gets refreshed.
I don't want to load the fragment/data again once it's already loaded.
Is it possible in Xamarin?
You can check if current fragment also executed Here is a function
public Fragment find(String identifier) {
Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentByTag(identifier);
return fragment;
}
and in BottomNavigationView you must check like this
if (find(fragment.getIdentifier()) != null) {
return;
}
I'd this problem sometime ago, however I was developing on native kotlin code, here goes the code I've used to handle this:
class Navigation(val supportFragmentManager: FragmentManager,
val filterIcon: ImageView ): BottomNavigationView.OnNavigationItemSelectedListener {
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.menu_bottom_home ->{
changeFragment(HomeFragment(), Constants.FRAG_HOME_FRAGMENT)
filterIcon.visibility = View.VISIBLE
}
R.id.menu_bottom_favorites ->{
changeFragment(FavoritesFragment(), Constants.FRAG_FAVORITES_FRAGMENT)
filterIcon.visibility = View.INVISIBLE
}
R.id.menu_bottom_reservations ->{
changeFragment(ReservationFragment(), Constants.FRAG_RESERVATION_FRAGMENT)
filterIcon.visibility = View.INVISIBLE
}
R.id.menu_bottom_profile ->{
changeFragment(ProfileFragment(), Constants.FRAG_PROFILE_FRAGMENT)
filterIcon.visibility = View.INVISIBLE
}
}
return true
}
fun changeFragment(fragment: Fragment, tag : String){
var contains: Fragment? = null
var current: Fragment? = null
if(supportFragmentManager.fragments != null && supportFragmentManager.fragments.isNotEmpty()) {
contains = supportFragmentManager.fragments.firstOrNull { x -> x.tag == tag }
current = supportFragmentManager.fragments.first { x -> x.isVisible }
}
if(current != null){
supportFragmentManager.beginTransaction()
.hide(current).commit()
}
if(contains != null){
supportFragmentManager.beginTransaction()
.show(contains).commit()
}
else{
supportFragmentManager.beginTransaction()
.add(R.id.fl_fragment_container, fragment, tag)
.addToBackStack(tag)
.commit()
}
}
}
Then you just have to set setOnNavigationItemSelectedListener with your Navigation class like this:
bnv_navigation.setOnNavigationItemSelectedListener(Navigation(supportFragmentManager, iv_filter_bar))
I have created RecyclerView in fragment inside activity, all are working good but when i do notifyDataSetChanged() to adapter from activity through interface,I got an error "lateinit property adapter has not been initialized" but I have already initialized adapter
class BuildingListFragment : Fragment(), MainActivity.EditInterface {
lateinit var adapter: BuildingListAdapter
private var mListener: OnFragmentInteractionListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater!!.inflate(R.layout.fragment_building_list, container, false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val buildingList = ArrayList<BuildingDetailModel>()
val alphaList = arrayOf("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "z")
for (i in alphaList.indices) {
val building = BuildingDetailModel()
building.buildingName = alphaList[i] + " Building"
buildingList.add(building)
}
//initialize adapter
adapter = BuildingListAdapter(buildingList)
// RV_Building_List.layoutManager = LinearLayoutManager(context, LinearLayout.VERTICAL, false)
RV_Building_List.adapter = adapter
RV_Building_List.layoutManager = object : LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false) {
override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State) {
super.onLayoutChildren(recycler, state)
//TODO if the items are filtered, considered hiding the fast scroller here
val firstVisibleItemPosition = findFirstVisibleItemPosition()
if (firstVisibleItemPosition != 0) {
// this avoids trying to handle un-needed calls
if (firstVisibleItemPosition == -1)
//not initialized, or no items shown, so hide fast-scroller
{
fastscroller.visibility = View.GONE
}
return
}
val lastVisibleItemPosition = findLastVisibleItemPosition()
val itemsShown = lastVisibleItemPosition - firstVisibleItemPosition + 1
//if all items are shown, hide the fast-scroller
fastscroller.visibility = if (adapter.itemCount > itemsShown) View.VISIBLE else View.GONE
}
}
fastscroller.setRecyclerView(RV_Building_List)
fastscroller.setViewsToUse(R.layout.recycler_view_fast_scroller__fast_scroller, R.id.fastscroller_bubble, R.id.fastscroller_handle)
}
override fun editClickFromMainActivity() {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
//at this line error is lateinit property adapter has not been initialized
if (adapter.getIsSelected()) adapter.setIsSelected(false) else adapter.setIsSelected(true)
}
override fun onDetach() {
super.onDetach()
mListener = null
}
override fun onResume() {
super.onResume()
}
interface OnFragmentInteractionListener {
// TODO: Update argument type and name
fun onFragmentInteraction(uri: Uri)
}
companion object {
// TODO: Rename and change types and number of parameters
fun newInstance(): BuildingListFragment {
val fragment = BuildingListFragment()
return fragment
}
}
}
My mainActivity
class MainActivity : AppCompatActivity() {
private val mOnNavigationItemSelectedListener =
BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_list -> {
val fragment = BuildingListFragment.Companion.newInstance();
addFragment(fragment, R.anim.slide_re_in, R.anim.slide_re_out)
return#OnNavigationItemSelectedListener true
}
R.id.navigation_map -> {
val fragment = BuildingMapFragment.Companion.newInstance();
addFragment(fragment, R.anim.slide_in, R.anim.slide_out)
return#OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
val fragment = BuildingListFragment.Companion.newInstance()
addFragment(fragment, R.anim.slide_re_in, R.anim.slide_re_out)
iv_Add.setOnClickListener {
var intent = Intent(this, AddBuildingMapsActivity::class.java)
startActivity(intent)
}
iv_edit.setOnClickListener {
(BuildingListFragment.newInstance() as EditInterface).editClickFromMainActivity()
}
}
/**
* add/replace fragment in container [framelayout]
*/
private fun addFragment(fragment: Fragment, slide_re_in: Int, slide_re_out: Int) {
supportFragmentManager
.beginTransaction()
.setCustomAnimations(slide_re_in, slide_re_out)
.replace(R.id.fragmentContainer, fragment, null)//fragment.javaClass.getSimpleName()
.addToBackStack(null)//fragment.javaClass.getSimpleName()
.commit()
}
override fun onBackPressed() {
super.onBackPressed()
Log.e("TAG", "TAG")
}
interface EditInterface {
fun editClickFromMainActivity()
}
}
please help me to solve this issue
thanks..
Your problem is that you're creating a new instance of the fragment when the click handler is being called in the main activity line
(BuildingListFragment.newInstance() as EditInterface).editClickFromMainActivity()
You will need to call the method on the actual fragment instance that's currently on screen. There's various ways of getting around this but I think the safest path for now is doing something like
iv_edit.setOnClickListener {
val fragment = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG) as? BuildingListFragment
fragment?.editClickFromMainActivity()
}
though this will mean that you must also use the same FRAGMENT_TAG in addFragment on the .replace(R.id.fragmentContainer, fragment, null) line (FRAGMENT_TAG instead of null)