Fragment still visible after pressing the back button - android

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.

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.

Handle OnBackPress from Fragment

I have 3 fragments Fragment1, Fragment2 and Fragment3 and navigation is like
Fragment1->Fragment2->Fragment3
But on back press From Fragment3 go back to Fragment2 after completing some task like from Fragment2. And from Fragment1 finish this activity.
what will be the best method to do this task.
As per your question just you have to add addToBackStack() method before commit() transaction.
for example:
FirstFragment firstFragment = new FirstFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.article_fragment, firstFragment)
.addToBackStack(null).commit();
Add second and third fragment same as above manner and just add code in onBackPressed() Override method.
for example:
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
Please follow the process.
1. When you add fragment add below code to your code
fragmentTransaction.addToBackStack(null);
Then back button handle from the activity.
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount()>0){
getFragmentManager().popBackStack();
}else {
super.onBackPressed();
} }
It will be working perfectly. Happy coding.
There is no need to handle OnBackPress inside Fragment. When you performing Fragment transaction, you can put your fragment to BackStack:
When there are FragmentTransaction objects on the back stack and the user presses the Back button, the FragmentManager pops the most recent transaction off the back stack and performs the reverse action (such as removing a fragment if the transaction added it).
More details you can get from this article.

Call fragment from activity on back pressed?

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

Android - How to exit application only when home page is reached?

I have a main activity that hosts a lot of fragments and a "home page" fragment. Every time I want to add a fragment, I will call this method inside the main activity.
public void addFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(R.id.fragmentContainer, fragment, "new_fragment");
ft.addToBackStack("new_fragment").commit();
}
Whenever the user presses the back button of the phone, I want the "home page" fragment to be the last page the user sees before he exits the application. How can I do that? I have tried this inside the onBackPressed() inside the MainActivity but it doesn't work (the back button is not working).
#Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentByTag("new_fragment");
//if fragment is home page fragment, exit application
//else pop the newest fragment or go to home page fragment
if (fragment == fragDefault) {
finish();
} else {
if(fm.getBackStackEntryCount() == 0){
//display home page fragment here
}
}
}
You can check your back stack when you press the back button/key.
When adding your 'home page' Fragment, don't put it to the back stack.
Add all other Fragments when needed and put them to the back stack.
When user press the back button/key, if the back stack is empty, exit app, else pop up Fragment from back stack.
int count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
finish();
}
else {
getFragmentManager().popBackStack();
}
Alternatively you might want to try this:
if (f instanceof HomePageFragmentClass) {
finish();
} else {
//f.dosomething();
}
*I have not tested it yet, let me know if it works.

Why OncreateView called in non stored fragments?

I have a MainActivity and two fragments in this activity like SignupFragment and MenuFragment.First i add SignupFragment in MainActivity:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, new SignupFragment());
ft.commit();
Important: i don't need to store this fragment in backstack
Now from SignupFragment i replace MenuFragment
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.beginTransaction()
.replace(R.id.content_frame, new MenuFragment(), FRAGMENT_TAG)
.addToBackStack(null)
.commit();
Important: i need to add this fragment to backstack.For example i have another framgent like AnotherFragment and i replace it in MenuFragment and add that to backstack too and when user in AnotherFragment pressed back button i want to return him to MenuFragment
I override OnBackPressed in MainActivity and also add a listener for OnBackStackChangeListener:
FragmentManager fm = getSupportFragmentManager();
fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() == 0)
finish();
}
});
Problem When i'm in MenuFragment and press back button then i go back to SignupFragment andOnCreateView called for a little moment and then finish() called.( when i press back button , SignupFragmen's view shows for a moment and then destroyed )
Questions
1- How can i prevent to returning back to SignupFragment ?
2- Why OncreateView called in SignupFragment when back button pressed?
Thanks in advance.
I found my problem.
when i pressed back button, OnBackPressed called and also MenuFragment is visible yet then onCreateView of SignupFragment called and then onBackStackChanged called , So i check this in OnBackPressed:
if(getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG).isVisible()){
finish();
}

Categories

Resources