Is it possible to always navigate to main fragment on back button click?
I tried something like this
#Override
public void onBackPressed() {
super.onBackPressed();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStackImmediate();
}
It doesn't works.
So basically every time someone click the back button it should display Launcher Fragment and if user is already at Laucner fragment it should exit the app
Related
I have 5 fragments (home, settings, newPost, notifications and profile) inside my Main2Activity.
Inside HomeFragment I have a RecyclerView with a lot of post views, every post containing an "open" button which should open a PostFragment. After the PostFragment is opened I have 3 options to go back to HomeFragment: by tapping phone's 'back' button, tapping my post's XML "back" button or tapping Home from the bottomNavView. The problem is that after PostFragment disappears and my screen goes back to HomeFragment, PostFragment is still observable, but under HomeFragment's RecyclerView. How can I manage that?
This is how I open my PostFragment:
viewHolder.itemView.findViewById(R.id.seePostButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment newFragment = new PostFragment();
FragmentTransaction transaction = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_host_fragment, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
UPDATE:
It seems PostFragment will fully disappear if I press back one more time but it's not very efficient.
UPDATE 2.0:
public void onBackPressed(){
getActivity().getSupportFragmentManager().popBackStack();
}
Using this method:
1.if I'm in PostFragment and tap Home, PostFragment remains observable under HomeFragment's RecyclerView
2.if I'm in PostFragment and tap phone's back button, the problem is not happening anymore
3.if I'm in PostFragment and tap posts's XML back button, to problem does not happen anymore
BUT, in my ProfileFragment I have a 2tabs (MyPosts and Posts I've Applied To) TabView connected to a viewPager that contains the posts. If I open one post and tap any of the three options I have mentioned earlier, HomeFragment pops up, having the Post observable under the recyclerView. From this point, any bottomNavItem I tap, the PostFragment will be observable.
HomeFragment with PostFragment being visible between posts:
https://imgur.com/61d6PCd
PostFragment:
https://imgur.com/vZ8omUx
ProfileFragment:
https://imgur.com/TKzgXik
Tapping back from a PostFragment opened from ProfileFragment should get me back to ProfileFragment, not HomeFragment.
UPDATE 3:
I modified onBackPressed() to:
FragmentTransaction transaction =
getActivity().getSupportFragmentManager().beginTransaction();
transaction.remove(this);
transaction.addToBackStack(null);
transaction.commit();
Now if I tap Post's back button (not phone's) the Post is not visible anymore behind the RecyclerView. But if I use phone's back button, the problem is still there. I have to somehow include onBackPressed() in phone's Back button code..
UPDATE: Final edit, problem solved:
Because the Overrideable onBackPressed is only available in the activity that hosts the fragments I had to modify it there like this:
#Override
public void onBackPressed() {
if(getSupportFragmentManager().findFragmentByTag("POST") == null){
super.onBackPressed();
}else{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.remove(getSupportFragmentManager().findFragmentByTag("POST"));
transaction.addToBackStack(null);
transaction.commit();
}
}
I am developing an android in which when I pressed device back button I go to previous fragment. What I want to achieve is that when I pressed device back button I don't want to go to previous fragment. How can I achieve that?.
You must be doing calling addtobackstack("name") while adding or replacing the fragment.Remove this function before calling next fragment you wont go back to previous fragment for further desc
Do not add that fragment to backstack of the new fragment you're calling. Look at the below example I've used to explain you how this works.
To make that fragment come again, just add that fragment to backstack which you want to come on back pressed,
Eg:
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new LoginFragment();
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = ((FragmentActivity)getContext()).getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack("SignupFragment");
ft.commit();
}
}
});
In the above case, I m opening a LoginFragment when signIn button is pressed, right now am in SignupFragment. So if I call addToBackStack(TAG), TAG = "SignupFragment", then when back button is pressed in LoginFragment, we come to SignUpFragment.
Happy Coding!
I am using navigation drawer and fragment to show the component on selecting navigation option.And if user select navigation drawer option a fragment will open. So my problem is when I call a activity from fragment and when I press back button it call previous fragment but the content of that fragment remain same. What I want is when I press back button from activity it should open my fragment or recall fragment and values should be blank like first time we call the fragment.
Thanks.
Try this:-
#Override
public void onBackPressed() {
if (getTitle().equals("Title")) {
YourFragment fragment =new YourFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
} else {
super.onBackPressed();
}
}
I want to have a Fragment which provides the ability to create someting.
After that i want to show the new "someting" in another fragment. After pressing the back button on the device i want to go back to the MainFragment and not to the CreateFragment (this works well). But after that the ShowFragment is still visible.
Here is my code:
In my MainActivity i got a MainFragment which has a button "Create".
After tap the button i load a "Create" Fragment.
fragmentManager.beginTransaction()
.replace(R.id.container, CreateFragment.newInstance())
.addToBackStack("Create")
.commit();
If the user has entered some details he taps the "Ok" Button. This fires the following on the MainActivity.
fragmentManager.beginTransaction()
.replace(R.id.container, ShowFragment.newInstance(id))
.commit();
So far so good, but here comes the problem.
If the user taps the back button on the device he gets back to the MainFragment BUT the ShowFragment is still visible (under the MainFragment).
Update
This is what happens:
MainFragment > CreateFragment > ShowFragment > (BACK Button) > MainFragment (ShowFragment in the back)
Just pop the ShowFragment from the stack on the onBackPress Event as below:
#Override
public void onBackPressed() {
final Fragment fragment = fragmentManager.findFragmentById(R.id.container);
if (fragment != null) {
fragmentManager.popBackStack();
} else {
super.onBackPressed();
}
}
Press the back button of fragment ShowFragment within your ShowFragment fragment will call the onBackPressed() method. Then when you call method popBackStack(), it will return you back to the CreateFragment.
Sample code -
public void onBackPressed()
{
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();
}
See the post how-to-back-to-previous-fragment-on-pressing-manually-back-button-of-indivisual-fragment for more info with similar situation.
I got the next FragmentActivity. This FragmentActivity inflates a layout with a LinearLayout(a menu where some buttons are defined to call other fragments) and a FrameLayout (a black space where the other fragments are loaded depending the button I select).
public class MenuViewActivity extends FragmentActivity {
....
I load the selected Fragment using onClickListener:
protected void onCreate(Bundle savedInstanceState) {
....
final OnClickListener fragment_1 = new OnClickListener() {
#Override
public void onClick(View v) {
fragment_1 Fragment = new fragment_1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, Fragment);
transaction.addToBackStack(null);
transaction.commit();
}
};
I do this for about 5 buttons. This menu also contains a custom back key. The functionality of this back button should be like:
[fragment_1][fragmen_2][fragment_3][fragment_4][fragment_5]
The app starts always showing fragment_1. I con go from fragment_1 to any of the other fragments. So I could go for example from [fragment_1] to [fragment_4]. When pressing the back key I should go back to [fragment_1].
To detail a little bit more the functionality, I could do: [fragment_1]->[fragment_2]->[fragment_3] and when pressing back, should go back to [fragment_1].
I've got a onClickListener for the back key, but I don't know how to implement this functionality.
you're looking for this
getSupportFragmentManager().popBackStack();
also if you're unaware that's the default functionality for the actual android back button, so unless you've overrode it that will do the same thing.
You just need to pop all fragments from your backstack, unless you reach the one you want. It is probably simplest to use FragmentManager's popBackStack():
public abstract void popBackStack (int id, int flags)
Pop all back stack states up to the one with the given identifier