Я использовал шаблон Drawer Layout от Android Studio. Я хочу начать другое действие (startActivityForResult), когда я нажимаю на элемент меню. Я добавляю navController.addOnDestinationChangedListener
, вызываю другой метод и начинаю свою новую деятельность. Проблема, с которой я сталкиваюсь, заключается в том, что я выполнил finish()
в своей новой деятельности. Это возвращается на макете фрагмента.
Мне нужно снова щелкнуть меню ящика, чтобы вернуться к основному макету. Это как мой фрагмент в стеке. Вы можете убить его где-нибудь?
Вот часть моего кода:
public class MainActivity extends AppCompatActivity{
private Spinner mspinner;
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
//Toast toast = Toast.makeText(getApplicationContext(),"test",Toast.LENGTH_LONG);
//toast.show();
if (destination.getId() == R.id.nav_slideshow){
Lister();
}
}
});
}
public void Lister(){
Intent i = new Intent(this,EnregistrerActivity.class);
i.putExtra("name","test");
startActivityForResult(i,1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
public void onActivityResult(int code, int res, Intent resultat) {
if (code == 1){
if (res == RESULT_OK) {
Toast toast = Toast.makeText(getApplicationContext(),"test",Toast.LENGTH_LONG);
toast.show();
}
}
}
public class SlideshowFragment extends Fragment {
private SlideshowViewModel slideshowViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
slideshowViewModel =
ViewModelProviders.of(this).get(SlideshowViewModel.class);
View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
final TextView textView = root.findViewById(R.id.text_slideshow);
slideshowViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
}