简体   繁体   English

使用片段作为父活动

[英]Use fragment as parent activity

I'm writing an android app which uses a drawer navigation in which the user can go to different activities. 我正在编写一个使用抽屉导航的android应用,用户可以在其中进行不同的活动。 Inside the activities, the user can launch a new child-activity and can come back to the original one via up navigation in the AppBar. 在活动内部,用户可以启动新的子活动,并可以通过AppBar中的向上导航返回到原始活动。

Until now, I normally launched the new activities via an intent after they were chosen in the drawer menu. 到目前为止,在抽屉菜单中选择新活动后,我通常会根据意图启动它们。 Then inside the chosen activity I created a new AppBar in onCreate(); 然后在选定的活动中,我在onCreate()中创建了一个新的AppBar;

However, I want to use the same Drawer for all Activities because I want the current activity, to be shaded in the menu and I also don't want to set up a new AppBar in many of the activites. 但是,我想对所有活动使用相同的抽屉,因为我希望当前活动在菜单中显示为阴影,并且我也不想在许多活动中设置新的AppBar。

I've read that the common way to this, is to make a BaseActivity and a base_layout, which hosts the AppBar and the Activities as a Fragment. 我已经读到,实现此目的的常用方法是制作一个BaseActivity和一个base_layout,它们将AppBar和Activity作为片段托管。 However, that's also not really suitable for me, since I can't declare styles in the manifest and upward navigation doesn't work. 但是,这也不太适合我,因为我无法在清单中声明样式,并且向上导航也不起作用。

How would you go about the problem? 您将如何解决这个问题?

If u want to make a navigation drawer that works for all fragment then 如果您想制作一个适用于所有片段的导航抽屉,则
make a navigation drawer inside the main activity(This will work as a parent activity) and u can open fragment from that navigation drawer 在主活动中创建一个导航抽屉(这将作为父活动),您可以从该导航抽屉中打开片段
Here is the tutorial 这是教程
https://www.journaldev.com/9958/android-navigation-drawer-example-tutorial https://www.journaldev.com/9958/android-navigation-drawer-example-tutorial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM