标签: android-navigation

如何使用导航控制器组件设置不同的工具栏?

我实际上不确定,如何使用导航控制器组件设置不同工具栏的正确方法或最佳实践

在我的应用程序中。我想设置 2 个不同的工具栏。

  1. 绿色工具栏
  2. 红色工具栏

两个不同颜色的工具栏。这只是为了简化案例,实际上我有多个工具栏

我正在使用导航控制器组件。目前在我的主活动中作为主持人,我使用此代码在主活动中设置了绿色工具栏

        setSupportActionBar(green_toolbar)
        supportActionBar?.setDisplayShowTitleEnabled(false)

        // set up top hierarchy destination
        val appBarConfiguration = AppBarConfiguration(setOf(
            R.id.destination_home,
            R.id.destination_search,
            R.id.destination_user_control,
            R.id.destination_create_event)
        )

        green_toolbar.setupWithNavController(navController,appBarConfiguration)
Run Code Online (Sandbox Code Playgroud)

那么使用导航控制器组件设置不同工具栏的最佳方法是什么?

我必须在我的主要活动中制作这两个不同的工具栏吗?或者我是否必须将 fragmentY 目的地(具有红色工具栏)设置为另一个活动而不是片段?

或者....我不知道....请帮忙:)

android android-navigation android-architecture-components android-jetpack android-architecture-navigation

19
推荐指数
2
解决办法
1万
查看次数

SavedStateHandle 如何提供导航参数?

在 Android Compose 导航中,会填充导航参数,SavedStateHandle然后可以在ViewModel. 我已经在实践中做到了这一点,并且这里详细概述了实现: https: //stackoverflow.com/a/69145748/12385518

我的问题是我找不到任何关于为什么/如何发生这种情况的文档。事实上,我什至找不到官方文档,甚至根本没有提到导航参数SavedStateHandle。我很想了解更多相关内容。我已阅读这些文档(以及更多文档),但在该主题上没有发现任何内容:

https://developer.android.com/topic/libraries/architecture/viewmodel-savedstate https://developer.android.com/reference/androidx/lifecycle/SavedStateHandled

这似乎是一种非常好的传递导航参数的技术,但是由于没有在任何地方提到它,所以我正在考虑过渡到另一种技术。

android android-navigation android-jetpack-compose

19
推荐指数
0
解决办法
2677
查看次数

Android:在背面按下返回上一个片段

我已经实现了Navigation Drawer,它是Activity的子类.我的应用程序中有很多片段.我的问题在这里

想象一下有3个片段:

Fragment_1:Fragment_2:Fragment_3

当我启动我的应用程序时,Fragment_1被加载当我点击Fragment_1上的一些组件时,我将导航到Fragment_2,依此类推.

所以它就像

Fragment_1> Fragment_2> Fragment_3

当我从Fragment_2按回键时,我被导航回Fragment_1但是当我从Fragment_3按回键时,我被导航回Fragment_1(而不是Fragment_2)

我在Back Key按下的应用程序中想要这样的东西

Fragment_1 <Fragment_2 <Fragment_3

我使用Fragment,FragmentManager,FragmentTransaction如下:

MyFragment fragment = new MyFragment();
FragmentManager fragmentManager = getFragmentManager();

fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null)commit();
Run Code Online (Sandbox Code Playgroud)

我尝试在我的MainActivity中覆盖onBackPressed():

@Override
public void onBackPressed() {


        getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        int count = getFragmentManager().getBackStackEntryCount();
        if (count == 0)
               super.onBackPressed();
    }
Run Code Online (Sandbox Code Playgroud)

android android-fragments back-stack android-navigation

18
推荐指数
2
解决办法
4万
查看次数

如何实现android工具栏后退按钮

我正在使用自定义工具栏.我需要添加回按钮.现在我使用此代码添加后退按钮.

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.setBackgroundColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back_arrow));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
Run Code Online (Sandbox Code Playgroud)

这很好用.我可以看到添加了后退按钮.但请考虑我在Fragment1中没有后退按钮的情况.现在我转到Fragment2,然后添加Back Button.从片段2我打开片段3然后我再次添加后退按钮.

现在,当我按下fragment3中的后退按钮返回到fragment2时,我必须检查片段堆栈以查看片段2中是否需要后退按钮.

当我们将片段推送到堆栈时,还有其他方法可以自动处理后退按钮吗?

android android-navigation android-toolbar

18
推荐指数
3
解决办法
3万
查看次数

Jetpack Compose 中的作用域状态

在所有应用程序中,总会有这三种状态范围: 状态

使用 Compose,可以通过以下方式实现“每个屏幕状态”:

NavHost(navController, startDestination = startRoute) {
    ...
    composable(route) {
       ...
       val perScreenViewModel = viewModel()  // This will be different from
    }
    composable(route) {
       ...
       val perScreenViewModel = viewModel()  // this instance
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

“应用程序状态”可以通过以下方式实现:

val appStateViewModel = viewModel()
NavHost(navController, startDestination = startRoute) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

但是对于“Scoped State”呢?我们如何在 Compose 中实现它?

android android-navigation android-viewmodel android-jetpack-compose

18
推荐指数
2
解决办法
2047
查看次数

ViewPager PagerObserver未注册

当我关闭屏幕并重新打开时,我的应用程序关闭.

Logcat告诉我这个错误的原因是指向我的CourseFragment.class中的java第60行,它扩展了Fragment.

mViewPager.setAdapter(infoTechPageAdapter);
Run Code Online (Sandbox Code Playgroud)

我在CourseFragment.class中实现了一个ViewPager,它扩展了Fragment.

这是我的代码:

public class CourseFragment extends Fragment {

public static final String ARG_POSITION_NUMBER = "course_number";

private int position;

public CourseFragment() {
    // Empty constructor required for fragment subclasses
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView;
    position = getArguments().getInt("position");

    String course = getResources().getStringArray(
            R.array.array_navigation_drawer)[position];

    getActivity().setTitle(course);

    rootView = inflater.inflate(R.layout.activity_drawer_fragment,
            container, false);

    return rootView;
}// end onCreateView

@Override
public void onStart() {
    super.onStart();

    ViewPager mViewPager = (ViewPager) getActivity().findViewById(
            R.id.pagerYear);

    Log.i("posit", String.valueOf(position));
    switch (position) { …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-viewpager android-pageradapter android-navigation

17
推荐指数
1
解决办法
6858
查看次数

如何在活动之间使用BottomNavigationView按下时突出显示项目?

我为我的应用程序添加了底部导航视图,但我需要在活动之间而不是片段之间的底部导航视图,因此我已将此代码添加到Java以用于我的所有3个活动.

当我在手机中选择"秒"或"第三"时,所有内容都是正确的,但问题是突出显示第一项.

我需要突出显示我按下的项目.

我已经使用了片段,它工作得很好,但我仍然是使用片段的初学者所以我正在使用活动.

第一个活动代码是:

BottomNavigationView mBottomNavigation;

    mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);

    mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.Nav_Second:
                    Intent Second= new Intent(First.this, Second.class);
                    startActivity(Second);
                    break;
                case R.id.Nav_Third:
                    Intent Third= new Intent(First.this, Third.class);
                    startActivity(Third);
                    break;
            }

            return true;
        }
    });


}}
Run Code Online (Sandbox Code Playgroud)

第二项活动是:

BottomNavigationView mBottomNavigation;

    mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);

    mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.Nav_First:
                    Intent First= new Intent(Second.this, First.class);
                    startActivity(First);
                    break;
                case R.id.Nav_Third:
                    Intent Third= new Intent(Second.this, Third.class); …
Run Code Online (Sandbox Code Playgroud)

java android android-navigation bottomnavigationview android-bottomnav

17
推荐指数
3
解决办法
2万
查看次数

Android 导航组件后退按钮不起作用

我在 android 中使用导航组件,我最初设置了 6 个片段。问题是当我添加一个新片段 (ProfileFragment) 时。

当我从开始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。相反,它只停留在我所在的片段中——后退按钮什么也不做。

这是我的navigation.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/dashboard_navigation"
    app:startDestination="@id/dashboardFragment"
    >

                <fragment
                    android:id="@+id/dashboardFragment"
                    android:name="com.devssocial.localodge.ui.dashboard.ui.DashboardFragment"
                    android:label="DashboardFragment"
                    >
                                <action
                                    android:id="@+id/action_dashboardFragment_to_newPostFragment"
                                    app:destination="@id/newPostFragment"
                                    app:enterAnim="@anim/slide_in_up"
                                    app:exitAnim="@anim/slide_out_down"
                                    app:popEnterAnim="@anim/slide_in_up"
                                    app:popExitAnim="@anim/slide_out_down"
                                    />
                                <action
                                    android:id="@+id/action_dashboardFragment_to_notificationsFragment"
                                    app:destination="@id/notificationsFragment"
                                    app:enterAnim="@anim/slide_in_up"
                                    app:exitAnim="@anim/slide_out_down"
                                    app:popEnterAnim="@anim/slide_in_up"
                                    app:popExitAnim="@anim/slide_out_down"
                                    />
                                <action
                                    android:id="@+id/action_dashboardFragment_to_mediaViewer"
                                    app:destination="@id/mediaViewer"
                                    app:enterAnim="@anim/slide_in_up"
                                    app:exitAnim="@anim/slide_out_down"
                                    app:popEnterAnim="@anim/slide_in_up"
                                    app:popExitAnim="@anim/slide_out_down"
                                    />
                                <action
                                    android:id="@+id/action_dashboardFragment_to_postDetailFragment"
                                    app:destination="@id/postDetailFragment"
                                    app:enterAnim="@anim/slide_in_up"
                                    app:exitAnim="@anim/slide_out_down"
                                    app:popEnterAnim="@anim/slide_in_up"
                                    app:popExitAnim="@anim/slide_out_down"
                                    />

                            ====================== HERE'S THE PROFILE ACTION ====================                                
                                <action
                                    android:id="@+id/action_dashboardFragment_to_profileFragment"
                                    app:destination="@id/profileFragment"
                                    app:enterAnim="@anim/slide_in_up"
                                    app:exitAnim="@anim/slide_out_down"
                                    app:popEnterAnim="@anim/slide_in_up"
                                    app:popExitAnim="@anim/slide_out_down"
                                    />
                            =====================================================================                                

                </fragment>



                <fragment
                    android:id="@+id/profileFragment"
                    android:name="com.devssocial.localodge.ui.profile.ui.ProfileFragment"
                    android:label="fragment_profile"
                    tools:layout="@layout/fragment_profile"
                    /> …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-navigation

17
推荐指数
4
解决办法
2万
查看次数

如何防止 BottomSheetDialogFragment 在导航到另一个片段后消失?

我正在NavigationComponent我的应用程序上使用。

我有一个特定的流程,点击BottomSheetDialogFragment应用程序的按钮后应该导航到另一个片段。但是当弹出该 Fragment 时,我需要导航回上一个BottomSheetDialogFragment.

由于某种原因,该请求BottomSheetDialogFragment被自动驳回。

Frag A : click on a button  
Frag A -> Dialog B : click on a button  
Frag A -> Dialog B -> Frag C : pop Frag C from the stack  
Frag A : Dialog B was automatically dismissed =;/  
Run Code Online (Sandbox Code Playgroud)

如何才能防止这种解雇呢?


问:为什么我需要BottomSheetDialogFragment不解雇?
A:我通过.a监听打开片段的结果LiveData。由于解雇,BottomSheetDialogFragment它永远不会收到结果。

android android-dialog android-dialogfragment android-navigation android-bottomsheetdialog

16
推荐指数
1
解决办法
4061
查看次数

Android TaskStackBuilder startActivities会在Samsung Galaxy S3上打开白屏并冻结

在某些手机上尝试使用该startActivities()方法尝试启动活动时遇到问题TaskStackBuilder(特别是三星Galaxy S3 - Android 4.3,三星Galaxy S3 Neo - Android 4.4.2)

以下是使用的代码段:

TaskStackBuilder stackBuilder = TaskStackBuilder.create(ConfirmAddressActivity.this);
stackBuilder.addParentStack(AddressesActivity.class);

Intent intent = AddressesActivity.createStartingIntent(
    ConfirmAddressActivity.this,
    true, Collections.EMPTY_LIST
);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

stackBuilder.addNextIntent(intent);
stackBuilder.startActivities();
Run Code Online (Sandbox Code Playgroud)

有缺陷的行为是,在执行stackBuilder.startActivities()它之后打开一个白色屏幕并保持冻结而不打开AddressesActivity.这不是一种常规行为,有时会发生.

正常行为是,执行后stackBuilder.startActivities()打开白屏(0.5秒 - 1秒)然后打开AddressesActivity,同时正确构建该活动的完整后栈.这是许多设备/平台下的正确行为.(HTC M9 Android 6.0,三星S2 - Android 4.1.2,三星Galaxy S6 - Android 5.1.1,LG G4 - Android 5.1.1)

这是定义活动的android清单片段:

<activity
    android:name=".activity.account.AccountActivity"
    android:label="My account"
    android:screenOrientation="portrait"
    android:parentActivityName=".activity.DashboardActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".activity.DashboardActivity" />
</activity>

<activity
    android:name=".activity.addresses.AddressesActivity"
    android:label="My addresses"
    android:screenOrientation="portrait"
    android:parentActivityName=".activity.account.AccountActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activity.account.AccountActivity" …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-navigation taskstackbuilder

15
推荐指数
0
解决办法
467
查看次数