[英]How to navigate back to activity from navigation drawer tabs?
I have a navigation drawer with 3 main tabs https://i.stack.imgur.com/SIjdx.jpg .我有一个带有 3 个主要选项卡的导航抽屉https://i.stack.imgur.com/SIjdx.jpg 。 In each of these tabs ie settings and the rest, I have implemented a top back button that returns to the home activity when clicked.
在这些选项卡中的每一个中,即设置和 rest,我已经实现了一个顶部后退按钮,单击该按钮返回主活动。 I have also tried to handle the bottom back button but the problem now is that if I click the bottom back button(of the phone gesture navigation) https://i.stack.imgur.com/RYW16.jpg , it only exits the app instead of returning back to the home Activity as supposed.
我也尝试过处理底部后退按钮,但现在的问题是,如果我单击底部后退按钮(手机手势导航) https://i.stack.imgur.com/RYW16.jpg ,它只会退出应用程序而不是按预期返回主 Activity。
I would like to make it return back to the home activity so that I can be able to navigate through the tabs using both the top and bottom back buttons, what can I do to correct this, please?我想让它返回到家庭活动,这样我就可以使用顶部和底部的后退按钮浏览选项卡,请问我该怎么做才能纠正这个问题?
I have checked for similar questions on this site and have found no solution yet.我在这个网站上检查了类似的问题,但还没有找到解决方案。
Here is my Settings code(i use the same code for the other tabs as well):这是我的设置代码(我对其他选项卡也使用相同的代码):
public class Settings extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.settings, container, false);
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
requireActivity().finish();
}
});
return view;
}
}
EDIT :编辑:
HomeActivity:家庭活动:
public class HomeActivity extends AppCompatActivity {
private DrawerLayout drawer;
// Last update time, click sound, search button, search panel.
TextView timeField;
MediaPlayer player;
ImageView Search;
ConstraintLayout searchbar;
EditText textfield;
// For scheduling background image change(using constraint layout, start counting from dubai, down to statue of liberty.
ConstraintLayout constraintLayout;
public static int count = 0;
int[] drawable = new int[]{R.drawable.dubai, R.drawable.norway, R.drawable.eiffel_tower, R.drawable.hong_kong, R.drawable.statue_of_liberty,
R.drawable.beijing, R.drawable.chicago, R.drawable.colombia, R.drawable.vienna, R.drawable.tokyo};
Timer _t;
private WeatherDataViewModel viewModel;
private AppBarConfiguration appBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// use home activity layout.
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Allow activity to make use of the toolbar
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// host 3 fragments along with bottom navigation.
final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
assert navHostFragment != null;
final NavController navController = navHostFragment.getNavController();
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
// remove up button from all these fragments
appBarConfiguration = new AppBarConfiguration.Builder(
R.id.main_id) // remove up button from all these fragments >> Keep up button in R.id.nav_setting, R.id.nav_slideshow
.setOpenableLayout(drawer)
.build();
// Hiding default Drawer fragment that has the BottomNavView
navigationView.getMenu().findItem(R.id.main_id).setVisible(false);
viewModel = new ViewModelProvider(this).get(WeatherDataViewModel.class);
// Trigger action to open & close navigation drawer
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar
, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
timeField = findViewById(R.id.textView9);
Search = findViewById(R.id.imageView4);
textfield = findViewById(R.id.textfield);
searchbar = findViewById(R.id.searchbar);
// find the id's of specific variables.
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
toggle.setToolbarNavigationClickListener(v -> {
// Enable the functionality of opening the side drawer, when the burger icon is clicked
toggle.setDrawerIndicatorEnabled(true);
navController.navigate(R.id.main_id);
});
navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
// Hide/show top search bar
if (destination.getId() == R.id.main_id) {
searchbar.setVisibility(View.VISIBLE);
toggle.setHomeAsUpIndicator(R.drawable.ic_baseline_arrow_back_24);
} else {
searchbar.setVisibility(View.GONE);
}
// Fragments that you want to show the back button
if (destination.getId() == R.id.settings_id || destination.getId() == R.id.ads_upgrade_id || destination.getId() == R.id.privacy_policy_id) {
// Disable the functionality of opening the side drawer, when the burger icon is clicked
toggle.setDrawerIndicatorEnabled(false);
}
});
// For scheduling background image change
constraintLayout = findViewById(R.id.layout);
constraintLayout.setBackgroundResource(R.drawable.dubai);
_t = new Timer();
_t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// run on ui thread
runOnUiThread(() -> {
if (count < drawable.length) {
constraintLayout.setBackgroundResource(drawable[count]);
count = (count + 1) % drawable.length;
}
});
}
}, 5000, 5000);
Search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// make click sound when search button is clicked.
player = MediaPlayer.create(HomeActivity.this, R.raw.click);
player.start();
getWeatherData(textfield.getText().toString().trim());
// make use of some fragment's data
Fragment currentFragment = navHostFragment.getChildFragmentManager().getFragments().get(0);
if (currentFragment instanceof MainFragment) {
((MainFragment) currentFragment).getWeatherData(textfield.getText().toString().trim());
}
}
private void getWeatherData(String name) {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<Example> call = apiInterface.getWeatherData(name);
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(@NonNull Call<Example> call, @NonNull Response<Example> response) {
try {
assert response.body() != null;
} catch (Exception e) {
Log.e("TAG", "No City found");
Toast.makeText(HomeActivity.this, "No City found", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(@NotNull Call<Example> call, @NotNull Throwable t) {
t.printStackTrace();
}
});
}
});
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
// Open/close drawer animation
}
}
@Override
protected void onPause() {
super.onPause();
if (viewModel.getMediaPlayer() != null)
viewModel.getMediaPlayer().pause();
}
@Override
protected void onResume() {
super.onResume();
if (viewModel.getMediaPlayer() != null) {
viewModel.getMediaPlayer().start();
viewModel.getMediaPlayer().setLooping(true);
}
}
@Override
public boolean onSupportNavigateUp() {
final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
assert navHostFragment != null;
final NavController navController = navHostFragment.getNavController();
// return NavigationUI.navigateUp(navController,drawer);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp(); // navigateUp tries to pop the backstack
}
}
Use the onBackPressed()
function to override the action of the back button.使用
onBackPressed()
function 覆盖后退按钮的操作。 Here's some sample code:这是一些示例代码:
override fun onBackPressed() {
val intent = Intent(this, HomeActivtiy::class.java)
startActivity(intent)
}
Write the action you want inside the onBackPressed()
function.在
onBackPressed()
function 中编写您想要的操作。
In order to return to the activity instead of existing the app when the bottom back button is clicked:为了在单击底部后退按钮时返回活动而不是现有应用程序:
Remove the below callback from all the relevant fragments like the SettingFragment:从所有相关片段(如 SettingFragment)中删除以下回调:
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
requireActivity().finish();
}
});
Navigation components will handle the back stack already, so no worries about that.导航组件已经处理了后台堆栈,所以不用担心。 A little issue will come up that when you back to the home activity;
当您回到家庭活动时,会出现一个小问题; you'll see the UP button instead of the burger icon;
你会看到向上按钮而不是汉堡图标; to fix this, you need to add
toggle.setDrawerIndicatorEnabled(true)
when you back within addOnDestinationChangedListener
要解决此问题,您需要在返回
addOnDestinationChangedListener
时添加toggle.setDrawerIndicatorEnabled(true)
So, in HomeActivity
:所以,在
HomeActivity
:
navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
// Hide/show top search bar
if (destination.getId() == R.id.main_id) {
searchbar.setVisibility(View.VISIBLE);
toggle.setHomeAsUpIndicator(R.drawable.ic_baseline_arrow_back_24);
toggle.setDrawerIndicatorEnabled(true); // <<< Add this line of code to enable the burger icon
} else {
searchbar.setVisibility(View.GONE);
}
// Fragments that you want to show the back button
if (destination.getId() == R.id.settings_id || destination.getId() == R.id.ads_upgrade_id || destination.getId() == R.id.privacy_policy_id) {
// Disable the functionality of opening the side drawer, when the burger icon is clicked
toggle.setDrawerIndicatorEnabled(false);
}
});
The sound should continue playing as you are not creating a brand new activity.由于您没有创建全新的活动,因此声音应继续播放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.