Fragments recreated with Jetpack's Android Navigation components - android

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.

Related

I want to keep AddTransactionFragment while I navigate to other fragment with BottomNavigation (Android, Kotlin)

My problem is illustrated by this screen record.
When I click the FAB on FinanceFragment, it will open AddTransactionFragment. But when I navigate to ReminderFragment and go back to FinanceFragment, the AddTransactionFragment is destroyed.
How could I -- similar to the YouTube app when one switches between fragments -- keep AddTransactionFragment visible while I navigate to another fragment?
I tried to add addToBackStack() but this turned out not to be what I was looking for.

Android programming back button

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.

Android Back Navigation proper

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.

How to close an App from a Fragment in Android?

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.

Shortcut button on A fragment to perform programmatically an action defined in B without showing it on UI

I have 3 fragments : A, B and C.
A contents a list of element, when you chose an element from the list, it loads B fragment. Inside B, i have a button showPreview, a click on that button loads the C fragment.
I already implemented all of this, and it's working pretty nice.
Now what i want is to add, in A fragment, a shortcut button on each item, to access the preview (C fragment) without showing B fragment on UI like in google play application, you can download an app by clicking on Three dots -> Install, without opening app detail page.
Presently, when user clicks on shortcut button, i load the B fragment first (shown in UI), after i call previewButton.performClick() to click programmatically on the showPreview button. But that's not what i want because i am obliged to show B first, let it load entirely before making a performClick().
I have read about FragmentTransaction methods (attach/detach, add/remove etc.), about fragment lifecycle, etc ... without solution.
So my questions are :
how can i load B fragment without showing it on the UI ?
If that's impossible, how to do the same thing as google play application ?
Finally I resolved the issue. My problem was that I haven't seperate Views from Model. I didn't have to use performClick() anyway, I didn't have to depend on Button click.
I resolved it by just creating a method which does nicely what I want, after I use this method on button click and after shortcut list choice.

Categories

Resources