如何从一个 activity 中的片段导航到另一个 activity 中的片段?
How to navigate from fragment in one activity to fragment in another activity?
我想将返回导航添加到工具栏。我需要从 activity 中的片段到另一个 activity 中的特定片段。
看起来有点像这样,其中每条橙色线都表示导航到新的 activity 或片段:
如何从 OtherActivity 从片段 B 移动到片段 A?
使用此代码更改您的片段
fragmentManager.beginTransaction().replace(R.id.container_body, new FragmentC()).commit();
并在自定义工具栏上显示导航添加
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true)
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
考虑以下步骤:
From Activity
1 holding Fragment
A ,你想直接加载 Fragment
B in Activity
2.
现在,我想先,然后你在Fragment A
中按下一个按钮,就可以直接转到Activity B
。
那么就是说,你一到Activity
就直接加载Fragment B
2.
由于您正在处理后退导航(我相信您的意思是 upNavigation
?),您可以覆盖以下内容:
但是要看清楚,因为如果你需要在Activity2中加载一个确切的片段,你需要知道一些:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("frag", "fragmentB");
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
如您所见,当您单击工具栏上的后退箭头时,我们会通过我们的意图传递一个值来标识我们要加载的片段。
接下来,在您的 Activity2
中,只需获取 intent extra
并进行切换或 if statement
:
@Override
public void onResume(){
super.onResume();
Intent intent = getIntent();
String frag = intent.getExtras().getString("frag");
switch(frag){
case "fragmentB":
//here you can set Fragment B to your activity as usual;
fragmentManager.beginTransaction().replace(R.id.container_body, new FragmentB()).commit();
break;
}
}
从这里开始,您的片段 B 应该会显示在 Activity 2.
现在您可以在内部处理同样的事情 Activity 2 来决定当用户单击返回主页箭头时去哪里!
我希望这可以帮助您了解。
注意:我考虑了接口方法,意识到没有必要,因为使用这种方法可以轻松完成!
要从一个 Activity 导航到另一个 Activity 的片段,使用 Kotlin 版本 1.4.0,例如,在按钮上调用点击监听器,它的工作原理如下:
binding.yourButton.setOnClickListener {
supportFragmentManager.beginTransaction().replace(R.id.yourLayout, NameOfYourFragment()).commit()
}
我想将返回导航添加到工具栏。我需要从 activity 中的片段到另一个 activity 中的特定片段。
看起来有点像这样,其中每条橙色线都表示导航到新的 activity 或片段:
如何从 OtherActivity 从片段 B 移动到片段 A?
使用此代码更改您的片段
fragmentManager.beginTransaction().replace(R.id.container_body, new FragmentC()).commit();
并在自定义工具栏上显示导航添加
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true)
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
考虑以下步骤:
From Activity
1 holding Fragment
A ,你想直接加载 Fragment
B in Activity
2.
现在,我想先,然后你在Fragment A
中按下一个按钮,就可以直接转到Activity B
。
那么就是说,你一到Activity
就直接加载Fragment B
2.
由于您正在处理后退导航(我相信您的意思是 upNavigation
?),您可以覆盖以下内容:
但是要看清楚,因为如果你需要在Activity2中加载一个确切的片段,你需要知道一些:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("frag", "fragmentB");
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
如您所见,当您单击工具栏上的后退箭头时,我们会通过我们的意图传递一个值来标识我们要加载的片段。
接下来,在您的 Activity2
中,只需获取 intent extra
并进行切换或 if statement
:
@Override
public void onResume(){
super.onResume();
Intent intent = getIntent();
String frag = intent.getExtras().getString("frag");
switch(frag){
case "fragmentB":
//here you can set Fragment B to your activity as usual;
fragmentManager.beginTransaction().replace(R.id.container_body, new FragmentB()).commit();
break;
}
}
从这里开始,您的片段 B 应该会显示在 Activity 2.
现在您可以在内部处理同样的事情 Activity 2 来决定当用户单击返回主页箭头时去哪里!
我希望这可以帮助您了解。
注意:我考虑了接口方法,意识到没有必要,因为使用这种方法可以轻松完成!
要从一个 Activity 导航到另一个 Activity 的片段,使用 Kotlin 版本 1.4.0,例如,在按钮上调用点击监听器,它的工作原理如下:
binding.yourButton.setOnClickListener {
supportFragmentManager.beginTransaction().replace(R.id.yourLayout, NameOfYourFragment()).commit()
}