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.
Related
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 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.
On my app I am using a Main Activity, which has a Navigation Drawer, and as the user go to an options from the drawer, it will change the fragment beging displayed accordingly to the option selected.
If the user hit's "Back button" several times, it will go back to a point in which it will reach my Main Activity, which is a blank and empty layout.
When I reach this point (my main activit, empty), I would like to exit the app, or, open the Navigation Drawer.
The problem is, I don't know any event that shows me that I am back to the Main Activity. I checked the onResume, but it's never called, which makes sense, since the main activity has never been stopped.
I thought perhaps there would be an event from the fragment manager that would be called on the Main Activity when a fragment was detached, and from there I could check if there was no fragment at all attached?
When you push your first fragment, add a tag to it. Something like
transaction.replace( R.id.rootContainer, firstFragment, "rootFragment");
Whenever user presses back button, you can get your rootFragment from FragmentManager
FirstFragment myFragment = (FirstFragment) getSupportFragmentManager().findFragmentByTag("rootFragment");
And check if this fragment is visible or not, by myFragment.isVisible(), if not, then keep popping the stack, if it is visible, it means user is on the first fragment. Now you can either quit the app, or show your menu drawer.
Good night good sir. Thank you for your tip.
I used a different approach, based on your repply, that gave me quite a few insights.
On main activity, I Overrided the onKeyDown and check if it was pressed the Back Button. If yes, then I check the size of my Back Stack.
Uppon that, I decide if I want to finish my application.
Also, thanks for the tip of "labeling" the fragments on the back stack, didn't know I could do that.
How do I go to previous screen from current screen in Android app? I know there is a back button on phone, but it takes me to beginning screen of my app and I want my buttons on app to work for going back to previous screen.
Back button indeed takes you to previously seen activity on screen, that launched the current one (not by means of back button). If back button takes you to beggining screen of your app means that navigation to your last activity was done from it. Try launching an activity from another one different from start activity.
What really can be problematic is ending application once at start activity by pressing back button and discovering the application switching to activity that lauched start activity (not by means of back button). In this case you should just call finish() inside onDestroy() listener method of your start activity.