I have made the one Main_Activity And made many Fragment.
For example I am open the First Fragment and Second Fragment open from the first.My question is if i pressed the back Button on device the application terminate.Can possible i pressed the back Button the Screen Back second Fragment to First again pressed the screen move on Main_Activity then app terminate?
You cannot directly open fragments. They need to be inside some activity.
If you want to control the action of pressing the back button, you need to override the onBackPressed() method of the activity.
Use getFragmentManager().popBackStack() to go back to previous fragment and override onBackPress() method if required.
Related
I wanna make an application that has multiple screens (screen1, screen2, and screen3), and for navigating between screens I wanna use bottom navigation bar. I wanna make it like Instagram app that can navigate back to the previous screen when the back button is pressed. I have made an app with navbar but when I press the back button the application is closed directly even after I navigate to other screen.
Override the onBackPressed() method of your activity, and write back button logic on it.
I have two fragments (for ex. fragmentA and fragmentB).
first , in fragmentA use findNavController().navigate(R.id.action_fragmentA_to_fragmentB) to navigate to fragmentB .
then , in fragmentB if you want to back to fragmentA. there are two ways in below :
just press back button : fragmentA's onCreate() won't be called
findNavController().navigate(R.id.action_fragmentB_to_fragmentA) : fragmentA's onCreate() will be called
why?
The reason that the back button doesn't call onCreate of the fragment is by design. Users would not expect the back button to call the onCreate, or create, your fragment again.
As an example, think about when you open the YouTube app on Android and you are shown your home screen, populated with videos based on your interests. When you tap on a video after a bit of scrolling and then midway through the video decide to go back by pressing the back button, you expect the app to go back where you tapped on the video, with the same amount of scrolling you had done, instead of reloading your entire home page again filling with new videos and resetting you to the top of the screen.
Similarly, back button in your app should do the same. If however, you want your back button to behave differently, android does provide a way to do this. Refer to this for that.
I'm trying out Android's new Navigation Editor for the first time and I'm not sure if this is a missing feature, intentional omission, or if I'm missing something. I have two fragments and I want the first fragment to be able to navigate to the second one, but I want the activity to finish if back is pressed from either fragment.
With my current setup, I can navigate from mainFragment to newFragment. If I press back from the mainFragment, the activity finishes. The only piece I can't figure out is how to finish the activity when back is pressed from newFragment. I've tried every combination of Pop Behavior settings, but haven't achieved what I'm looking for.
Just set clearTask to "true" on your action.
But your use case is going against the concept of the navigation.
https://developer.android.com/topic/libraries/architecture/navigation/navigation-principles#the_app_should_have_a_fixed_starting_destination
Apps have a fixed destination which is the screen the user sees when they launch your app from the launcher. This destination should also be the last screen the user sees when they return to the launcher after pressing the back button.
See the screenshot and look for Pop Behavior. This option can be used to finish activity.
Please note: Finish activity = pop the Activity off the stack.
Select the action from the Activity to be finished, in navigation graph.
Look for drop down for Pop To.
Select the fragment(i.e. the navHostFragment of the activity to be finished).
Check Inclusive option. (i.e. From current destination point- in ur case, it's an action - to and including this fragment - in ur case navHostFragment of Activity- in the stack will be popped off the stack. And that's what we need!).
I am calling Activity2 from Activity1 through intent and on pressing back button I want my activity2 not to get destroyed and go back to Activity1 and again from activity1 want to start activity2.
I have tried to override the back button but then by using moveTaskToBack(true)
is taking me back to my phones home screen but i want to come back to previous activity.
Activities (and Fragments) are designed to destroy when onBackPressed is called. Since the user has no intention to proceed interaction with the screen anymore.
I think the best approach of your navigation should be use of Fragments with manually handled navigation. When Activity starts initialize both Fragments. When you'll need to open second screen, just replace current fragment with Fragment2. When onBackPressed is called - replace Fragment2 with Fragment1. In this case both fragments are always available to be shown.
One thing to be aware of - state saving, since the system can destroy your app in the background anyway. In this case, save state manually when Activity is being destroyed.
I have used the concept of Fragments in my Android App. I want to close my App from a fragment Activity. It's easy to do it in normal Activities by using onBackPressed() function, but how to do it in Fragments?
Fragment never runs independent. Activity contain the fragment so if you want to go back just press back button.
If you have any button in fragment to do so then write getActivity().finish();inside button click method.
EDIT 1 if you want to push your app in background then write following code
getActivity().moveTaskToBack(true);
The user can quit your app by pressing the Home button or navigate using the Back Button. Having a button in your app which closes it goes against best practices suggested by Google.