back button issue in working with fragment in android - android

I am working on android app and use fragment for displaying user interface.
I am different fragment,i have two problem in app-
1. I am facing problem when user navigate from one fragment to another then multiple instance of same fragment is added in backstack.
Suppose user move from fragment1 to fragment2 then fragment3 and again move to fragment2 and thenpress back button then from backstack fragment3 is load and then fragment2 and then fragment1.so i just want to add a fragment one only in backstack.
I am creating only one instance of a fragment bu using my custom method for each fragment like-
public static Fragment getInstance(FragmentManager f){
if(mFragment==null){
mFragment = new HomeFragment();
}
fm = f;
return mFragment;
}
so each time when a fragment is replaced with a previous instance of same fragment.
2 after navigate to different screens when user presses virtual android mobile home button and app goes to sleep and after few hours if user again opens app then app force close.
Thanks in advance

Related

How to know from what fragment I'm returning when using Navigation Component?

I have four fragments already set up in my app using Navigation Components:
I can go from MainFragment to MoviesFragment or TrailersFragment as well as from the UserFragment to one of them.
From the MainFragment I can simply navigate to the MoviesFragment fragment using this code:
ActionMainFragmentToMoviesFragment action = actionMainFragmentToMoviesFragment();
action.setFromMainFragment(true);
Navigation.findNavController(rootView).navigate(action);
To navigate to the TrailersFragment fragment, I'm using similar code. When I use:
.setFromMainFragment(true);
In MoviesFragment for instance, I can simply get a boolean from the Bundle so I can know from which fragment I'm coming from, either from MainFragmnet or UserFragment and it workd very well.
And here comes the problem. When I want to get from MoviesFragment back to MainFragment, I don't navigate as above, I just press the back button. So the problem is that I don't know in the MainFragment from which fragment I'm returning (MoviesFragment or TrailersFragment).
What should I do to know from which fragment I'm coming from in MainFragment?

Activity Buttons still ACTIVE when replaced by Fragment

Basically, I want to open a fragment from Activity. Everything worked fine (activity button opens the fragment and the fragment shows), until I miss-clicked outside the fragment's layout where one of the Activity button is located and it did its onClick method. That's why I wondered why is my Activity still active.
this is how I open the fragment from Activity
I tried using .add instead of .replace still the same.
#OnClick(R.id.login_register_lbl)
public void registerAction(View view) {
// TODO register transition...
FragmentManager fragmentManager = (LoginActivity.this.getFragmentManager());
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
RegisterFragment fragment = new RegisterFragment();
fragmentTransaction.replace(R.id.fragment_register_holder, fragment);
fragmentTransaction.commit();
}
In fragment layout in root element set "background" , "clickable=true" & Focusable="true".
In my opinion you can achieve this functionality by 2 ways:
Open a dialog fragment in full screen mode to avoid accidentally clicks on activities views.
If you wanted to show both fragment's and activity's views at the same time then, on click of your activity's button which opens the fragment, disable it by yourbutton.setenabled(false); and when you wanted to close the fragment enable that button again.

No view found for id when popping backstack without adding to backstack

I am working on an Android app and have encountered an error involving Fragments and FragmentTransactions. I have created an example app to demonstrate the problem. Here is what I am doing:
add Fragment1 to fragmentSpace, without adding to backstack.
replace fragmentSpace with Fragment2, adding to backstack as "beginning".
add Fragment3 to fragmentSpace2 (which is inside of Fragment2), without adding to backstack.
replace fragmentSpace2 with Fragment4, adding to backstack as null.
Call getFragmentManager().popBackStack("beginning", FragmentManager.POP_BACK_STACK_INCLUSIVE); in an attempt to undo all the Transactions, which should bring the user back to seeing only Fragment1.
However, the app crashes on step 5 with this error:
java.lang.IllegalArgumentException: No view found for id 0x7f090004 (me.MitchT.testfragmenttransactions:id/fragmentSpace2) for fragment Fragment3{7f35cb6 #2 id=0x7f090004}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:886)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:1645)
...
I have found that if I add step 3 to the backstack, step 5 no longer crashes the app and there is no more error. Why?
The problem I have with adding step 3 to the backstack is that I want the user to be able to press the back button after Fragment3 is added and go back to Fragment1. If I add it to the backstack, then when the user presses the back button it removes Fragment3, but Fragment2 is still visible, so they must press the back button again. I am trying to avoid that double-back-button behavior.
I did not feel it was necessary to post the code for every single file in the example app on this question, so instead I created a gist.
So I guess my questions are:
Why do I get this error?
Why don't I get the error after adding step 3 to the backstack?
Is there a better way of "going back to the beginning" or going back two fragments at a time?
Thanks!
From what i understand. You have a fragment layout which is the container in activity
Activity hosts Fragment1. Then you replace Fragment 1 by Fragment 2 ( added to back stack.
At this stage clicking back button you will have fragment2 popped and you see the activity which hosts fragment1.
Fragment2 has a framelayout which holds fragment3. Then you have Fragment4 replacing fragment3 which is added to back stack.
Now clicking the button you have getFragmentManager().popBackStack("beginning", FragmentManager.POP_BACK_STACK_INCLUSIVE);. But there is no view with that id.
Just use popBackStack(). Fragment4 --> Fragment2 ( hosting fragment3) --> Fragment1 ( hosted by the activity).
If you want Fragment4 to Activity hosting Fragment1 don't add Fragment4 to the back stack.
Note : In case you want nested fragments you need to use getChildFragmentManager(). https://developer.android.com/about/versions/android-4.2.html
Also note the discussion in the comments sections and the link posted by op Is this the right way to clean-up Fragment back stack when leaving a deeply nested stack?

android add fragment advice

In my application I have two fragments A and B. A fragment contains Google map and listview inside on it B fragment Google map only.
I want open B fragment from A fragment but when I press back button it has to go to A fragment without loading data.
public void addPage(final DefaultFragment pDefaultFragment, final boolean isAddToBackStack){
FragmentTransaction transaction = mFragmentManager.beginTransaction();
transaction.add(R.id.content_frame, pDefaultFragment);
//transaction.replace(R.id.content_frame, pDefaultFragment);
if (isAddToBackStack) transaction.addToBackStack(null);
transaction.commitAllowingStateLoss();
}
I use this code for adding fragment
Bframent b = new Bfragment();
addPage(b,true);
I know differences between transaction. add and transaction.replace. My problem is when I use transaction.add A fragment's map doesn't destroy it stays above of B fragment's map when pressing button. But when use transaction.replace after press back button data loads again.
So could anyone tell me what should I do to Press back button without to load data without any problem on view also.
On back press just provide your fragment null;

Back key to pop the Fragment shows overlapping fragments

I created a sample app to test this overlapping issue.
I have a fragment type, Fragment1, and I create a new instance of Fragment1 and add it to a FrameLayout in my activity at runtime. I add the fragment with the help of a few buttons.
Note: I have given each new instance of Fragment1 a different number(#1, #2, #3, etc.) to display on the UI to help me figure out which fragment I am viewing.
So.. here is what I do:
Click on Button 3, create new instance of Fragment1 and add it to Frame1.
Click on Button 4, create new instance of Fragment1 and add it to Frame1 and add it to the fragment backstack.
Repeat 1 and 2.
Repeat 1 and 2.
Now, I have fragments in this order: 1(#1),2(#2),1(#3),2(#4),1(#5),2(#6).
I press the back key when viewing fragment #6.
Back key press, UI displays (#5).
Back key press, UI displays (#3 AND #5),
Back key press, UI displays (#1, #3, AND #5)
It seems fragments are getting displayed ON TOP of each other.
WHY? Is there an overlapping issue? How can I clear out this overlapping issue. I thought this would be an issue on the compatibility library... but it is also on 3.0.
Code for adding fragments:
public int doFragmentChange(int cont1, Fragment frag1, String tag1, int cont2, Fragment frag2, String tag2,
boolean addToStack, String stackTag) {
FragmentManager fm = getFragmentManager();// getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (frag1 != null) {
ft.replace(cont1, frag1, tag1);
}
if (frag2 != null) {
ft.replace(cont2, frag2, tag2);
}
// add fragment to stack
if (addToStack)
ft.addToBackStack(stackTag);
return ft.commit();
}
If you perform two add calls one after the other (two commit calls) then yes the fragments will appear overlaid, one on top of the other effectively.
So (for new example) if say you replace frag1 with frag2 and then frag3 with frag4 in the same frame with no backstack transaction then I would expect frag2 and frag4 to be overlaid.
Furtheremore there is also a potential issue in your chaining of replace. You should call a separate commit for each. See Android — Replace Fragment Back Stack With New Stack?.
Just override the onBackPress() or onKeyUp and remove the top fragment.

Categories

Resources