Closed fragment still appearing under base fragment - android

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();
}
}

Related

Return from activity to last visited intent

I have a main activity that exists out of three fragments. Fragment 2 is the main fragment. On fragment 3 I have a button. Once I click the button it directs the user to a ChatActivity. The ChatActivity has an onBackButtonPressed that should return the user back to fragment 3. However, it seems that it would always return the user to fragment 2 (the main fragment).
How can I bring the user to the fragment they last visited, or at least back to fragment 3?
Edit:
I added this block of code in the button onClick function:
ChatFragment fragment = new ChatFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.main_tabPager, fragment);
transaction.addToBackStack(null);
transaction.commit();
When I click the back button in the activity it does not return me to fragment 3 but instead rebuild the fragmentpager and start back at Fragment 2.
When you are opening fragment 3 from main fragment (fragment 2), add fragment 3 into backstack like this:
Fragment3 fragment3 = new Fragment3();
getSupportFragmentManager().beginTransaction().add(R.id.content, fragment3).addToBackStack(null).commit();
You should add all fragments to backstack that you want to return to
Ideally addToBackStack() on fragment transaction should be enough as per documentation, but it seems not true at times, so we have to handle the popping up of the back stack upon Back button pressed by ourselves. I added this to my activity and it worked as expected:
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0 ){
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
Hope it helps.

Android - prevent to open previous fragment

guys.
This must be a silly question but i'm not managing to work this out. My scenario is: i have in my MainActivity a BottomNavigation which i navigate over three fragments. And my problem is that when the back button (from android bottom navigation toolbar) is pressed the previous fragment opens but i want the app to close. So my question is: how i manage to prevent the previous fragments to open?
PS: I know it has something to do with FragmentMananger back stack but i did not understand how to use it.
PS2: Sorry for bad english.
The fragments are on the backstack.
Edit:
Work with FragmentTransaction and use addToBackStack (null)
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Override the onBackPressed() method in your activity.
#Override
public void onBackPressed() {
finish(); //This would close the app
}
Word of caution. This would close the activity in all the cases when the user presses back. To avoid this perhaps you would like to create something like this:
#Override
public void onBackPressed() {
if(someCondition) {
finish(); //This would close the activity
}
else {
super.onBackPressed(); //Fallbacks to default Android implementation
}
}

How to avoid Fragment going back to previous Fragment on pressing device back button?

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!

Fragment still visible after pressing the back button

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.

Switching between fragments with back button

Ok i have an activity with one main fragment, that has a menu on it. When a user clicks on a menu item another fragment is animated into the screen, with this code:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
ft.hide(getFragmentManager().findFragmentByTag("menu_fragment"));
Fragment opisFragment = getFragmentManager().findFragmentByTag("opis_fragment");
if (opisFragment == null) {
opisFragment = new OpisFragment();
ft.add(R.id.p_container, opisFragment, "opis_fragment");
ft.commit();
} else {
ft.show(opisFragment);
}
Note: pr_fragment is the tag of the current fragment, the one that has the menu.
Now, this works well, but when i'm on the second fragment i want to add the functionality, that when the user clicks the back button it will show the first fragment. With this code, when i click back it exits the activity alltogether.
Thank you for the help!
All you need is to use addToBackStack(String name) of FragmentTransaction
// Showing menu fragment also added in backstack
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out)
.add(R.id.p_container, menuFragment, "menu_fragment")
.addToBackStack("menu_fragment")
.commit();
// Showing opis fragment also added in backstack
FragmentTransaction ft2 = getFragmentManager().beginTransaction();
ft2.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out)
.add(R.id.p_container, opisFragment, "opis_fragment")
.addToBackStack("opis_fragment")
.commit();
Assuming "opis fragment" is in foreground, when you press back button, "menu_fragment" will be displayed back to the foreground, pressing back button again will exit the activity.
With this code, when i click back it exits the activity alltogether.
Normal because there is just your activity in your app stack. the addToBackStack() method is what you are looking for.
if (opisFragment == null) {
opisFragment = new OpisFragment();
ft.add(R.id.p_container, opisFragment, "opis_fragment");
ft.addToBackStack("tag"); // <<< this line
ft.commit();
}
From the doc :
Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.
in mainactivity you can check fragments count if fragments count more than one we will show back button
if(getSupportFragmentManager().getBackStackEntryCount() > 0)
{
mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
else
{
mDrawerToggle.setDrawerIndicatorEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}

Categories

Resources