从独立的fragment开始一个activity
我是 Android 开发的初学者,我使用 kotlin。我遵循了名为“ViewPager2 with Navigation Component”的启动画面和入门屏幕教程。每个屏幕都是一个片段,在入门屏幕之后,我最终会看到 HomeFragment,而不是 MainActivity。不过,我对这些片段并不熟悉。
现在,我想创建一些其他屏幕、活动,但不能。我认为这是因为主页片段不是活动的子项。这是我的导航图:
HomeFragment 的代码如下:
class HomeFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
fun toNextAct(view: View) {
/*
val intent = Intent(Context,NextActivity::class.java)
startActivity(intent)*/
}
}
我想使用带有 onClick 函数 toNextAct() 的按钮启动活动,如上所述。或者,至少我想以某种方式进入 MainActivity,因为它没有被使用。我该如何解决这个问题?
Edit1:activity_main.xml 中有一个 NavHostFragment,但 MainActivity 不是 HomeFragment 的父级。
关于评论的编辑2:这是我的activity_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:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/my_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
HomeFragment的.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=".HomeFragment">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="191dp"
android:text="myButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt文件:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.hide()
}
}
I am a beginner to Android development and I use kotlin. I followed a tutorial for splash and onboarding screens called "ViewPager2 with Navigation Component". Each screen is a fragment and after the onboarding screens, I end up at the HomeFragment and not the MainActivity. However, I am not familiar with the fragments.
Now, I want to create some other screens, activities, but cannot. I think that is because the home fragment is not a child of an activity. Here is my navigation graph:
The codes for the HomeFragment is as below:
class HomeFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
fun toNextAct(view: View) {
/*
val intent = Intent(Context,NextActivity::class.java)
startActivity(intent)*/
}
}
I wanted to start an activity using a button with onClick function toNextAct() as above. Or, at least I want to go to the MainActivity somehow because it is not used. How can I fix the problem?
Edit1: There is a NavHostFragment inside the activity_main.xml but MainActivity is not the parent of the HomeFragment.
Edit2 regarding to comments: Here is my activity_main.xml file:
<?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:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/my_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
The HomeFragment's .xml file:
<?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=".HomeFragment">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="191dp"
android:text="myButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
The MainActivity.kt file:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.hide()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的活动上下文是仅在片段中可供您使用的目录(您可以简单地复制粘贴并使用此代码)
here activity context is directory available to you in fragment only (you can simply copy paste and use this code)
如果您要从片段移动到活动,则应该使用 navcontroller() 。将 Activity 包含在导航文件中,然后从片段名称引导它,然后您应该在 onclick 中调用它,就像这样
binding.next.setOnClickListener{ findNavController().navigate(R.id.action_screen1Fragment_to_MainActivity) }
you should use navcontroller() if you are using to move from fragment to an activity. include the activity in your navigation file and then direct it from whatever your fragment name is and then you should call it in your onclick just like this
binding.next.setOnClickListener{ findNavController().navigate(R.id.action_screen1Fragment_to_MainActivity) }