android-target-instructions库跳过按钮失效,求助实现剩余步骤跳过
问题描述
我正在尝试集成android-target-instructions库,希望实现点击跳过按钮来跳过剩余引导步骤。根据库文档提示调用.finish()方法,但没有任何效果。我尝试重新加载Fragment,但引导步骤仍然存在,必须在点击跳过按钮时将其关闭。
现有代码
private fun showInstructions() { val userCredentials = CommonUtilities.getUserTokenAndName(requireActivity()) val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(requireActivity()) if (!userCredentials.first.isNullOrEmpty() && !userCredentials.second.isNullOrEmpty() // && CommonUtilities.isHomeFirstTimeOpening(requireActivity()) ) { val target1 = SimpleTarget.Builder(requireActivity()) .setTarget(binding.oscarChiefIc) .setTitle("Floating Action Button") .setHighlightRadius(100f) // Circle shape .setDescription("Discover culinary inspirations - click to explore recipes and ingredients.") .setMessageLayoutResId(R.layout.instruction_layout) // This is custom layout .build() val target2 = SimpleTarget.Builder(requireActivity()) .setTarget(binding.scanIcon) .setTitle("Floating Action Button") .setMessageLayoutResId(R.layout.instruction_layout) // This is custom layout .setHighlightRadius(100f) // Circle shape .setDescription("Scan all your products and add it to your cart.") .build() val inflater = LayoutInflater.from(requireContext()) val popupView: View = inflater.inflate(R.layout.popup_message_view, null) val width = LinearLayout.LayoutParams.WRAP_CONTENT val height = LinearLayout.LayoutParams.WRAP_CONTENT val focusable = false val popupWindow = PopupWindow(popupView, width, height, focusable) val skipBtn = popupView.findViewById<Button>(R.id.skipBtn) val targets = arrayListOf(target1, target2) view?.post { popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0) } skipBtn.setOnClickListener { popupWindow.dismiss() parentFragmentManager.beginTransaction() .replace(R.id.container_frameLayout, NewHomeFragment()).addToBackStack(null).commit() } TargetInstructions.with(requireActivity()) .setTargets(targets) .start() } sharedPrefs.edit().putBoolean(HOME_FIRST_RUN, false).apply() }
解决方案
问题核心是你没有持有TargetInstructions的实例引用,导致无法调用.finish()方法终止引导流程。按以下方式修改代码:
- 保存TargetInstructions实例:将链式调用拆分,用变量保存
TargetInstructions实例,避免调用.start()后丢失引用。 - 在跳过按钮点击时执行finish():在
skipBtn的点击事件中,先调用实例的.finish()方法关闭引导,再处理弹窗关闭和Fragment跳转。
修改后的代码:
private fun showInstructions() { val userCredentials = CommonUtilities.getUserTokenAndName(requireActivity()) val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(requireActivity()) if (!userCredentials.first.isNullOrEmpty() && !userCredentials.second.isNullOrEmpty() // && CommonUtilities.isHomeFirstTimeOpening(requireActivity()) ) { val target1 = SimpleTarget.Builder(requireActivity()) .setTarget(binding.oscarChiefIc) .setTitle("Floating Action Button") .setHighlightRadius(100f) // Circle shape .setDescription("Discover culinary inspirations - click to explore recipes and ingredients.") .setMessageLayoutResId(R.layout.instruction_layout) // This is custom layout .build() val target2 = SimpleTarget.Builder(requireActivity()) .setTarget(binding.scanIcon) .setTitle("Floating Action Button") .setMessageLayoutResId(R.layout.instruction_layout) // This is custom layout .setHighlightRadius(100f) // Circle shape .setDescription("Scan all your products and add it to your cart.") .build() val inflater = LayoutInflater.from(requireContext()) val popupView: View = inflater.inflate(R.layout.popup_message_view, null) val width = LinearLayout.LayoutParams.WRAP_CONTENT val height = LinearLayout.LayoutParams.WRAP_CONTENT val focusable = false val popupWindow = PopupWindow(popupView, width, height, focusable) val skipBtn = popupView.findViewById<Button>(R.id.skipBtn) val targets = arrayListOf(target1, target2) // 保存TargetInstructions实例 val targetInstructions = TargetInstructions.with(requireActivity()) .setTargets(targets) view?.post { popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0) } skipBtn.setOnClickListener { popupWindow.dismiss() // 调用finish()终止引导流程 targetInstructions.finish() parentFragmentManager.beginTransaction() .replace(R.id.container_frameLayout, NewHomeFragment()).addToBackStack(null).commit() } // 启动引导 targetInstructions.start() } sharedPrefs.edit().putBoolean(HOME_FIRST_RUN, false).apply() }
额外注意事项:
- 确认自定义布局
instruction_layout中的确认按钮正确关联库的回调,保证引导流程能正常推进。 - 检查
HOME_FIRST_RUN的逻辑,确保首次启动判断准确,避免引导重复触发。
内容的提问来源于stack exchange,提问作者PotatoUser
相关产品推荐
相关产品推荐

