当平板电脑被纵向握持时,我在活动中有3个片段.但是在景观中我只有2个这样的片段.我遇到的问题是当从纵向到横向时,活动正在创建第3个片段.我收到并收到错误,因为无法创建此片段.
我已经知道这个片段正在创建,因为它位于后栈中.
我试图通过使用删除onDestroy方法中的片段
FragmentTransaction f = fragmentManager.beginTransaction();
f.remove(mf);
f.commit();
Run Code Online (Sandbox Code Playgroud)
但是我得到一个错误,说我不能在onSaveInstanceState之后使用这个函数
将这个片段从后栈中取出的正确方法是什么?
我应该补充一点,我遇到问题的片段是来自这个库的mapFragment
https://github.com/petedoyle/android-support-v4-googlemaps
我使用它的方式就是这样
mf = MapFragment.newInstance(1, true);
ft = fragmentManager.beginTransaction();
ft.replace(R.id.mapContainer, mf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack("map");
ft.commit();
Run Code Online (Sandbox Code Playgroud) 我想知道如何使用导航控制器正确处理系统后退按钮操作.在我的应用程序中,我有两个片段(例如片段1和片段2),我在fragment1中有一个目标为fragment2的动作.一切都很好,除了一件事 - 当用户按下fragment2中的系统后退按钮时,我想显示一个对话框(例如使用DialogFragment)来确认退出.实现此行为的最佳方法是什么?如果我app:defaultNavHost="true"在我的主机片段中使用它会自动返回忽略我的规则.此外,这个组件是什么?
我应该使用"pop to"吗?
基本上,我有以下导航图:
我希望fragment 2在到达之后将导航图中的起点更改为正确(为了防止fragment 1在按下后退按钮时返回 - 就像启动画面一样).
这是我的代码:
navGraph = navController.getGraph();
navGraph.setStartDestination(R.id.fragment2);
navController.setGraph(navGraph);
Run Code Online (Sandbox Code Playgroud)
但是,显然它不起作用,fragment 1按下后退按钮后又回来了.
我做错了吗?还有其他解决方案吗?
android android-navigation android-jetpack android-architecture-navigation
我正在尝试新的导航架构组件,我无法弄清楚如何做到这一点:
我有1个活动(MainActivity)+ 3个碎片:
我想使用SplashFragment来确定我是否应该导航到MainFragment或SignUpFragment,但是一旦它到达那两个中的任何一个,你就不应该回弹到SplashFragment.我怎么能用新的导航组件做到这一点?
我popBackStack在调用之前和之后尝试过navigate(R.id.action_xxx),但是它们都没有工作(这有意义:在它没有任何东西可以弹出之前;它只是关闭刚刚添加的片段).这是否意味着唯一的方法是覆盖onBackPress拦截它并确保navigateUp在这些情况下不会调用?
谢谢!
我有2个动作
动作1
<action
android:id="@+id/actionBaseFragmentToAskForLocation"
app:destination="@+id/introAskForLocationFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
Run Code Online (Sandbox Code Playgroud)
动作2
<action
android:id="@+id/actionIntroAskLocationToLogin"
app:destination="@id/loginFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_right"
app:popExitAnim="@anim/fade_out"
app:popUpTo="@+id/app_main_navigation" />
Run Code Online (Sandbox Code Playgroud)
我想要的是当第二个动作被触发时,我想清除后堆栈并仅将loginFragment设置为保留在堆栈中。
唯一的问题是当我执行Action2时,将“ slide_out_right”作为退出动画执行
我知道,如果我们从堆栈中弹出片段,则将触发action1的“ popExitAnim”,而不是action2的“ exitAnim”。
但是我想知道如何让片段执行slide_out_left动画以退出并同时将其弹出堆栈。
android android-navigation android-architecture-components android-architecture-navigation
在我的应用程序中,我使用了一个活动和四个片段。在这个应用程序中,我使用了导航组件。我的片段加载顺序是 A->B->C->D
在片段 D 按下设备后退按钮中,我想从堆栈中删除片段 C,以便片段 B 可以打开。任何人都建议我该怎么做 提前谢谢
这是我的 navigation_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<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/navigation_graph"
app:startDestination="@id/firstFragment"
>
<fragment
android:id="@+id/firstFragment"
android:name="com.education.javaprogramming.fragments.MainFragment"
android:label="fragment_first"
tools:layout="@layout/activity_main" >
<action
android:id="@+id/action_firstFragment_to_displayList1Fragment"
app:destination="@id/displayList1Fragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<action
android:id="@+id/action_firstFragment_to_displayList2Fragment"
app:destination="@id/displayList2Fragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<action
android:id="@+id/action_firstFragment_to_helpFragment"
app:destination="@id/helpFragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
<action
android:id="@+id/action_firstFragment_to_quizzFragment"
app:destination="@id/quizzFragment"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit"
/>
</fragment>
<fragment
android:id="@+id/displayList1Fragment"
android:name="com.education.javaprogramming.fragments.DisplayList1Fragment"
android:label="fragment_display_list1"
tools:layout="@layout/fragment_display_list1"
>
<argument
android:name="position1"
app:argType="integer" />
<action
android:id="@+id/action_displayList1Fragment_to_firstFragment"
app:destination="@id/firstFragment" />
<argument
android:name="name"
app:argType="string" />
<action
android:id="@+id/action_displayList1Fragment_to_displayList2Fragment" …Run Code Online (Sandbox Code Playgroud) 使用导航架构组件,在某些情况下我们还可以通过如下代码进行动态导航:
\n\nnavController.navigate(R.id.cartFragment)\nRun Code Online (Sandbox Code Playgroud)\n\n然后,当我们需要从该目的地导航到新的根目的地(后退导航将关闭应用程序)时,我们无法知道当前的根目的地是什么,因此我们不知道将 popUpTo 目的地设置为什么。
\n\nval navigationOptions = NavOptions.Builder().setPopUpTo(R.id.???, true).build()\nnavController.navigate(R.id.loginFragment, null, navigationOptions)\nRun Code Online (Sandbox Code Playgroud)\n\n如果我们将其设置为不在后台堆栈中的目的地,那么我们会收到警告日志(来自popBackStackInternalin NavController):
\n\n\n“忽略 popBackStack 到目标 *:id/splashFragment,因为在当前返回堆栈上找不到它”
\n
发生这种情况是因为我们之前在流程中也有多种情况,我们将 PopUpTo 设置为依赖于流程,因此我们有不同的根目的地。
\n\n我已经查看了所有可用的方法NavController并尝试了一些反思mBackStack,但我无法找到清除后台堆栈的方法。
在不知道当前根目标时如何清除后退堆栈?\n
\n编辑:添加导航图示例\n
<fragment\n android:id="@+id/navigation_cart"\n android:name="ProjectsFragment_"\n android:label="Cart"\n tools:layout="@layout/fragment_cart" />\n\n<fragment\n android:id="@+id/loginFragment"\n android:name="LoginFragment_"\n android:label="Login"\n tools:layout="@layout/fragment_login">\n <--! Dynamic navigation to either Consent or Cart based on state -->\n <action\n android:id="@+id/action_login_to_home"\n app:destination="@id/navigation_cart"\n app:popUpTo="@id/loginFragment"\n app:popUpToInclusive="true" />\n <action\n android:id="@+id/action_loginFragment_to_consentFragment"\n app:destination="@id/consentFragment" />\n <action\n android:id="@+id/action_loginFragment_to_contactFragment"\n app:destination="@id/contactFragment" />\n</fragment>\n\n<fragment\n …Run Code Online (Sandbox Code Playgroud) 从此处开始使用导航架构时,不推荐使用此处的clearTask。
我的场景是这样的:有 2 个屏幕登录和注册,两者都有彼此的链接。所以你可以从登录进入注册,也可以从注册登录。但在后面 Press App 应该关闭。
只需将 clearTask 添加到以下两个操作即可轻松完成。
<?xml version="1.0" encoding="utf-8"?>
<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/nv_auth_phase"
app:startDestination="@id/fragment_login">
<fragment
android:id="@+id/fragment_login"
android:name="com.jobhook.ui.auth.login.LoginFragment"
android:label="LoginFragment"
tools:layout="@layout/fragment_login">
<action
android:id="@+id/nv_action_login_to_registration"
app:clearTask="true"
app:destination="@id/fragment_registration" />
</fragment>
<fragment
android:id="@+id/fragment_registration"
android:name="com.jobhook.ui.auth.registration.RegistrationFragment"
android:label="RegistrationFragment"
tools:layout="@layout/fragment_registration">
<action
android:id="@+id/nv_action_registration_to_login"
app:clearTask="true"
app:destination="@id/fragment_login" />
</fragment>
</navigation>
Run Code Online (Sandbox Code Playgroud)
但是由于它已被弃用,我尝试了其他解决方案,例如添加popUpTo -> 导航图的 Id,使两个操作中的 launchSingleTop 都为 true。在我的场景中似乎没有任何效果。
我也检查了这个问题,但没有得到解决方案。
android android-fragments android-architecture-components android-architecture-navigation
我正在使用导航体系结构组件,并且具有类似于此的设置,用于在导航到特定片段时弹出堆栈:
<action
android:id="@+id/navigate_to_main_screen"
app:destination="@id/fragment_main_screen"
app:popUpTo="@+id/navigation_main"
app:popUpToInclusive="true"/>
Run Code Online (Sandbox Code Playgroud)
这几乎可以预期。应用程序栏中的系统后退按钮和向上图标都不会导航到上一个片段。系统后退按钮退出应用程序。
但是,应用程序栏中的向上按钮仍然存在,单击该按钮不会执行任何预期的操作。我究竟做错了什么?为什么还在这里?
在主要活动中,我已经有
AppBarConfiguration config =
new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupActionBarWithNavController(this, navController, config);
Run Code Online (Sandbox Code Playgroud)
和
@Override
public boolean onSupportNavigateUp() {
return navController.navigateUp() || super.onSupportNavigateUp();
}
Run Code Online (Sandbox Code Playgroud)
根据文档。
我正在使用的库版本:
implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha09'
implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha09'
Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments android-architecture-navigation
我正在使用 Android 导航组件并面临一个奇怪的问题。无论我尝试什么,我都无法清除向上按钮的返回堆栈。
我的导航图如下所示:
让我们以 LoginFragment 为例,
<fragment
android:id="@+id/loginFragment"
android:name="com.yashovardhan99.firebaselogin.LoginFragment"
android:label="Login"
tools:layout="@layout/fragment_login" >
<action
android:id="@+id/action_loginFragment_to_welcomeFragment"
app:destination="@id/welcomeFragment"
app:popUpTo="@+id/nav_graph"
app:popUpToInclusive="true" />
</fragment>
Run Code Online (Sandbox Code Playgroud)
要导航的 Java 代码是:
navController.navigate(
LoginFragmentDirections.actionLoginFragmentToWelcomeFragment());
Run Code Online (Sandbox Code Playgroud)
我已将 PopUpTo 设置为图形 ID,并将 Inclusive 设置为 true。移动到 WelcomeFragment 时,这应该清除返回堆栈。但是,WelcomeFragment 仍然在操作栏上显示向上按钮,按下它会将我带回 PreLoginFragment(这是图表的主目的地)。奇怪的是,按后退按钮让我按预期退出了应用程序。
android android-fragments android-jetpack android-architecture-navigation
我正在尝试做什么
我正在使用 Android 导航组件来处理我的应用程序中的导航。在此示例中,我有两个屏幕,屏幕 A 和屏幕 B。我希望用户能够单击屏幕 A 中的按钮并能够导航到屏幕 B;然后无法返回到上一屏幕(屏幕 A)。
问题
当用户从屏幕 A 导航到屏幕 B 时,操作栏上的后退按钮仍然允许用户返回上一屏幕,但是当单击底部栏中的后退按钮时,它会退出应用程序,因此这部分工作正常。
我需要做什么才能删除操作栏中的后退按钮?
到目前为止我读过的内容
我已遵循这三篇文章中的指导,但我认为他们可能忽略了ActionBar后退按钮:
我的代码
导航图 - nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/screen_a">
<fragment
android:id="@+id/screen_a"
android:name="com.example.conditionalnavigation.AFragment"
android:label="screen A">
<action
android:id="@+id/action_AFragment_to_BFragment"
app:destination="@id/screen_b"
app:launchSingleTop="true"
app:popUpTo="@id/screen_a"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/screen_b"
android:name="com.example.conditionalnavigation.BFragment"
android:label="screen B" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
MainActivity - 这充当我的单一活动导航主机。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { …Run Code Online (Sandbox Code Playgroud) android kotlin back-stack android-navigation android-jetpack-navigation
android ×11
android-architecture-navigation ×7
android-architecture-components ×3
back-stack ×1
fragment ×1
kotlin ×1
navigation ×1