英文:
FAB on click start new activity based on fragment currently shown
问题
我正在尝试找到一种从浮动操作按钮点击开始活动的好方法。
我的浮动操作按钮位于mainactivity.xml中。我的MainActivity包括带有片段的底部导航。我需要根据显示的片段实施不同的点击操作。
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_add);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fabClicked();
}
});
fabClicked
方法
public void fabClicked() {
Object id = getSupportFragmentManager().findFragmentById(R.id.container);
if ((getSupportFragmentManager().findFragmentById(R.id.fragment_1)).equals(id)) {
Intent intent1 = new Intent(MainActivity.this, Add1.class);
startActivity(intent1);
} else if ((getSupportFragmentManager().findFragmentById(R.id.fragment_2)).equals(id)) {
Intent intent2 = new Intent(MainActivity.this, Add2.class);
startActivity(intent2);
} else if ((getSupportFragmentManager().findFragmentById(R.id.fragment_3)).equals(id)) {
Intent intent3 = new Intent(MainActivity.this, Add3.class);
startActivity(intent3);
} else if ((getSupportFragmentManager().findFragmentById(R.id.fragment_home)).equals(id)) {
//计划添加代码以在主屏幕上隐藏浮动操作按钮
}
}
英文:
I am trying to figure a good way to start an activity from a click on the floating action button.
My FAB is located in mainactivity.xml. My MainActivity consists of bottom navigation with fragments. I need to implement a different on click action determined by a fragment that is shown.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_add);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fabClicked();
}
});
fabClicked
Method
public void fabClicked() {
Object id = getSupportFragmentManager().findFragmentById(R.id.container);
if ((getSupportFragmentManager().findFragmentById(R.id.fragment_1)).equals(id)) {
Intent intent1 = new Intent(MainActivity.this, Add1.class);
startActivity(intent1);
}
else if ((getSupportFragmentManager().findFragmentById(R.id.fragment_2)).equals(id)) {
Intent intent2 = new Intent(MainActivity.this, Add2.class);
startActivity(intent2);
}
else if ((getSupportFragmentManager().findFragmentById(R.id.fragment_3)).equals(id)) {
Intent intent3 = new Intent(MainActivity.this, Add3.class);
startActivity(intent3);
}
else if ((getSupportFragmentManager().findFragmentById(R.id.fragment_home)).equals(id)) {
//plan to add to code to hide fab on homescreen
}
}
答案1
得分: 0
一个简单的解决方案是:
每当使用底部导航时,只需将所选的片段 ID 保存在静态变量中,当用户按下 FAB 时,根据存储在变量中的片段打开活动。
英文:
One simple solution for this is :
Whenever the bottom navigation is used, just save the selected fragment id in a static variable and when the user presses the FAB, just open the activity based on the fragment stored in variable.
答案2
得分: 0
你的方法很不错,但是它进行了许多不必要的昂贵的片段查找。
你可以使用以下代码获取当前片段的导航ID:
navController.getCurrentDestination().getId()
然后,你可以使用返回的ID在单击侦听器中根据片段执行不同的操作,就像这样:
fun fabClicked() {
val id = navController.getCurrentDestination().getId()
val clazz = when(id) {
R.id.fragment_1 -> Add1::class.java
R.id.fragment_2 -> Add2::class.java
// 等等
else -> null
}
clazz?.let {
startActivity(Intent(this, it))
}
}
通过这种方式,你不需要为每个条件进行片段查找,只需简单比较ID。你还可以提取共享的 startActivity
逻辑(如上所示),使代码更加简洁。
英文:
Your approach is good, but it's doing many unnecessary expensive Fragment lookups.
You can get the navigation id of the current Fragment with:
navController.getCurrentDestination().getId()
You can then use this returned id to do different things in the click listener based on the Fragment, like so:
fun fabClicked() {
val id = navController.getCurrentDestination().getId()
val class = when(id) {
R.id.fragment_1 -> Add1::class
R.id.fragment_2 -> Add2::class
// etc
}
startActivity(Intent(this, class.javaClass))
}
In this way, you don't need to do Fragment look-ups for each condition, just simply compare ids. You can also extract the shared startActivity
logic (as shown here) to make this much more concise.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论