我正在编写一个有两种"模式"的应用程序; 一种视图模式和一种编辑模式.该应用程序公开了两个主要/启动器活动(比如A和D),这些活动又有自己的活动链(分别为B,C和E,F).两个入口点活动A和D将在主屏幕中显示两个带有单独标签的图标,并且两个链从不交叉,即一旦您在具有活动A的查看模式下启动应用程序,您可以来回走动的唯一路线是A,B,C,如果您在具有活动D的编辑模式下启动应用程序,则唯一可用的活动链是D,E,F.
现在,我的问题是如果启动应用程序,例如查看模式(活动链A,B,C)并按下该链中任何活动的主页按钮,我会回到主屏幕(当然)但是如果我那么在编辑模式下重新启动应用程序(活动链D,E,F)我按下主页按钮(即错误链中的活动)时进入我正在进行的活动 - 而不是编辑模式的预期入口点; 活动d.
我该如何解决这个问题?
我曾尝试各种组合android:noHistory,android:clearTaskOnLaunch和其他属性在AndroidManifest.xml为参与活动.但它们似乎只影响了活动,而不是整个链条.
当我按下Home按钮时,我想从历史堆栈中删除整个活动链(A,B,C或D,E,F)但是当我还在链中时仍然保持堆栈完整(我想要能够从活动B按下后退按钮并进入活动A …
我遇到了问题,我无法解决.在我的应用程序中,我有一个导航抽屉定义如下:
activity_main.xml中
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed …Run Code Online (Sandbox Code Playgroud) android android-layout android-listview android-fragments android-navigation
我的问题:
我想在导航抽屉中添加可拖动图标.
抽屉打开时,此图标与导航抽屉列表视图相结合.
看到类似这样的,
我试过的,
我在StackOverflow中搜索过类似的问题,
但所有答案都建议第三方图书馆.
我的问题
1.可以在导航抽屉中添加可拖动图标吗?
android android-custom-view navigation-drawer android-navigation android-navigationview
所以,我有4个活动活动Activity1,Activity2,Activity3和Activity4.我Activity1从那时开始,在某些事件上我开始Activity2然后在一些事件上Activity2我开始Activity3作为一项新任务
public void onClick(View v) {
Intent intent = new Intent(Activity2.this, Activity3.class);
intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
这里Activity2launchMode被标记为singleTask.
然后我导航Activity3到Activity4
当我开始时Activity2,Activity4我相信它应该有像它一样的背斜
任务A.
|活性2 |
|活动1 |
任务B.
| Activity4 |
| Activity3 |
而是将新实例Activity2添加到当前任务中
任务B.
|活性2 |
| Activity4 |
| Activity3 |
任务A.
|活性2 |
|活动1 |
请有人帮我理解这个吗?
出于某种原因,我总是遇到以下错误:
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment androidx.navigation.fragment.NavHostFragment: make sure class name exists, is public, and has an empty constructor that is public
Run Code Online (Sandbox Code Playgroud)
我正在遵循Android 开发人员指南,但在启动应用程序后立即发生上述错误。
导航图.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="projects.ferrari.rene.sken.ui.main.MainFragment"
android:label="main_fragment"
tools:layout="@layout/main_fragment" >
<action
android:id="@+id/action_mainFragment_to_takeImageFragment"
app:destination="@id/takeImageFragment" />
</fragment>
<fragment
android:id="@+id/takeImageFragment"
android:name="projects.ferrari.rene.sken.ui.takeimage.TakeImageFragment"
android:label="take_image_fragment"
tools:layout="@layout/take_image_fragment" />
Run Code Online (Sandbox Code Playgroud)
活动_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
主活动.kt
package projects.ferrari.rene.sken
import androidx.appcompat.app.AppCompatActivity
import …Run Code Online (Sandbox Code Playgroud) android android-navigation android-architecture-components android-jetpack
我正在使用在Google I/O 2018上呈现的Android导航,似乎我可以通过绑定到某个视图或使用NavHost从Fragment获取它来使用它.但我需要的是根据几个条件从我的第一个片段导航到ViewModel中的另一个特定视图.因为ViewModel,我延伸AndroidViewModel,但我不明白下一步该怎么做.我无法转换getApplication为Fragment/Activity而我无法使用NavHostFragment.此外,我不能只导航到导航,onClickListener因为startFragment只包含一个导航ImageView.我该如何导航ViewModel?
class CaptionViewModel(app: Application) : AndroidViewModel(app) {
private val dealerProfile = DealerProfile(getApplication())
val TAG = "REGDEB"
fun start(){
if(dealerProfile.getOperatorId().isEmpty()){
if(dealerProfile.isFirstTimeLaunch()){
Log.d(TAG, "First Time Launch")
showTour()
}else{
showCodeFragment()
Log.d(TAG, "Show Code Fragment")
}
}
}
private fun showCodeFragment(){
//??
}
private fun showTour(){
//??
}
}
Run Code Online (Sandbox Code Playgroud)
我的片段
class CaptionFragment : Fragment() {
private lateinit var viewModel: CaptionViewModel
private val navController by …Run Code Online (Sandbox Code Playgroud) 我正在 Android 中进行一些 Espresso 测试。测试失败并出现此错误:
java.lang.ClassCastException: androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity 无法转换为 com.stavro_xhardha.pockettreasure.MainActivity
这是我的测试方法:
@Test
fun toolbarTitle_shouldContainCorrectInput() {
val mockNavController = mock(NavController::class.java)
val fragmentScenario = launchFragmentInContainer<SetupFragment>()
fragmentScenario.onFragment {
Navigation.setViewNavController(it.view!! , mockNavController)
}
onView(withId(R.id.toolbar)).check(matches(withText("Pick your country")))
}
Run Code Online (Sandbox Code Playgroud)
但是错误不是来自 Test 类,而是来自我的 Fragment under test 。崩溃是在这行代码中执行的:
override fun onStart() {
super.onStart()
(activity!! as MainActivity).supportActionBar?.hide() //here
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是,当我在没有测试的情况下正常运行应用程序时,我没有遇到任何错误。
android android-fragments android-testing android-navigation android-espresso
我有一个包含 NavHostFragment 的 BottomSheetDialogFragment,如下所示:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/add_feeling_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:navGraph="@navigation/add_feeling_graph" />
</LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下任一方法在其类中获取 NavController 时:
activity?.findNavController(R.id.add_feeling_nav_host_fragment)
view.findNavController()
Run Code Online (Sandbox Code Playgroud)
第一个选项崩溃,因为它找不到 ID,而第二个选项确实找到了父导航控制器并使用它。
我为另一个 Fragment 设置了相同的设置,但它不是一个对话框,而且运行良好。有什么想法可能是什么问题?谢谢
android android-dialogfragment android-navigation android-architecture-navigation
我目前正在尝试使用 Android 导航组件从一个片段 (A) 导航到另一个 (B),方法是从屏幕底部(在片段 A 上)向上动画片段 B,然后在按下后退按钮时将片段 B 向下动画被按下。我目前为此定义了一个操作:
<action
android:id="@+id/action_landingFragment_to_bookingFragment"
app:destination="@id/bookingFragment"
app:enterAnim="@anim/booking_screen_in"
app:exitAnim="@anim/nothing"
app:popEnterAnim="@anim/nothing"
app:popExitAnim="@anim/booking_screen_out"
app:popUpTo="@id/landingFragment" />
Run Code Online (Sandbox Code Playgroud)
然而,当我当前从片段 A 导航到片段 B 时,我从屏幕底部平滑地向上滑动,点击后退按钮并看到片段 B 立即消失以再次显示片段 A,没有动画。我目前没有为片段 B 上的后退按钮使用自定义代码(尽管我尝试navigateUp()并popBackStack()在 anOnBackPressedCallback中查看我是否遗漏了有关这些的内容。我还想知道片段的 Z 轴是否存在问题(类似于此线程) ) 但我的正常进入/退出动画工作正常,我尝试了一组更传统的动画(向左/向右滑动),其中进入和退出动画播放,但弹出动画再次不播放。
任何人都可以提出正确的返回导航方法,以便这些流行动画应该可见吗?
android android-fragments android-navigation android-architecture-navigation
我很好奇如何使用 Android Jetpack 中的导航图从抽屉布局中的菜单项导航到嵌套导航图。我知道幕后有一些魔法可以将菜单项与基于 Id 的片段链接起来,但我不知道如何将菜单项链接到嵌套导航图。
例如,我使用 Android Studio 附带的默认导航抽屉活动项目。我已将 mobile_navigation.xml 修改为以下内容:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.example.testdrawer.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />
<include app:graph="@navigation/nested_navigation" />
<fragment
android:id="@+id/nav_tools"
android:name="com.example.testdrawer.ui.tools.ToolsFragment"
android:label="@string/menu_tools"
tools:layout="@layout/fragment_tools" />
<fragment
android:id="@+id/nav_share"
android:name="com.example.testdrawer.ui.share.ShareFragment"
android:label="@string/menu_share"
tools:layout="@layout/fragment_share" />
<fragment
android:id="@+id/nav_send"
android:name="com.example.testdrawer.ui.send.SendFragment"
android:label="@string/menu_send"
tools:layout="@layout/fragment_send" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
我还添加了一个名为nested_navigation.xml 的新导航图,如下所示:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nested_navigation"
app:startDestination="@+id/nav_gallery">
<fragment
android:id="@+id/nav_gallery"
android:name="com.example.testdrawer.ui.gallery.GalleryFragment"
android:label="@string/menu_gallery"
tools:layout="@layout/fragment_gallery" />
<fragment
android:id="@+id/nav_slideshow"
android:name="com.example.testdrawer.ui.slideshow.SlideshowFragment"
android:label="@string/menu_slideshow"
tools:layout="@layout/fragment_slideshow" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
假设我的菜单如下所示:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_menu_camera"
android:title="@string/menu_home" />
<item …Run Code Online (Sandbox Code Playgroud) android android-navigation android-drawer android-jetpack android-jetpack-navigation
android ×10
android-architecture-navigation ×2
android-architecture-components ×1
back-stack ×1
kotlin ×1
mvvm ×1