How to close an App from a Fragment in Android? - 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.

Related

How to make a backpressed action for navigating to the previous fragment in activity that implement bottom navbar?

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.

Fragments recreated with Jetpack's Android Navigation components

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.

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.

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.

Moving between android activities on button clicks

I am writing a android application where, on startup activity view, I have a button "DoIt". When user clicks "DoIt" button, I start another activity with different layout. On newly started activity, I have a button "Back" which should take me to the first activity. How to accomplish this. What code should I write on OnClick method of "Back" button. Also, I want the newly created activity to die after back button is pressed and application comes back to start-up activity.
In the new activity you can just call
this.finish();
to return to the previous activity. If you want a result from the child activity you have to launch it with startActivityForResult() and override onActivityResult in the parent. The hard back key should always go back to the parent activity by default.
Call finish() on your activity. Also, why are you making a button on screen for this? This is usually the job of the device's back button.
In my opinion, Android is really bad on such scenario. In Activity, it doesn't support multiple views. Consider the situation that users want to switch from these two views, or even several other views? I think in this case, iPhone is much much better.

Categories

Resources