removing fragment with popBackStack(); - java

I have a few fragments in one activity. In Main Activity I implemented popBackStack(); in onBackPressed();
So that, you can always go back to previous fragment clicking back button. But popBackStack(); doesn't remove the fragment I just left.
How to achieve removing current fragment each time, we click back
button?

public void removeFragmentbyTag(String myFrag){
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(myFrag);
trans.commit();
manager.popBackStack();
}
hope this bit of code help you.

Related

Opening a fragment which replaces nav_host with being able to go back

I am trying to show a new fragment where the nav_host_fragment is but I want the old one to show whenever I click on backButton or on an X in my app (I added that already).
In the state that it is in right now it won't let me get out of the PlaylistFragment.
I also tried around with FragmentTransaction.add(...) but I couldn't figure how I get this working
My Code:
MainActivity.java:
Fragment fragment = new PlayListFragment(position);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment oldFragment = fragmentManager.findFragmentByTag(fragment.getClass().getName());
//if oldFragment already exits in fragmentManager use it
if (oldFragment != null) {
//fragment = oldFragment;
}
fragmentTransaction.replace(R.id.nav_host_fragment, fragment, fragment.getClass().getName());
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.addToBackStack("playlist");
fragmentTransaction.commit();
PlaylistFragment.java:
getActivity().getSupportFragmentManager().popBackStack("playlist", 0);
EDIT:
When I tried putting null instead of "playlist" in the backstack:
Yup. Then I see both fragments overflowing each other:
Normal state:
https://prnt.sc/12yitg5
When clicking on the "downloaded songs":
https://prnt.sc/12yiu79
When clicking back in the fragment:
https://prnt.sc/12yiuy6
EDIT: When I click on the X again it shows me the home fragment while bottom nav highlights the Library tab.

Return to a fragment already created

I'm working on an Android application that uses fragments.
The code is supposed to do the following:
I'm in the mainActivity;
In here there's a button that creates a fragment;
Turn on an on/off button;
I go back to the mainActivity;
I click on the button and go to the created fragment;
The on/off button is turned on;
I tried to implement this and it doesn't work. In step 5 I believe that it's creating a new fragment everytime, because the on/off button is always turned off.
Basically want I want is to go back to the same fragment that I created firstly.
This is the current code that I have:
MainActivity:
public void onStartSensors(View view) {
if(count > 0){
getSupportFragmentManager().popBackStack();
}
else{
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.container, new StartupFragment());
transaction.addToBackStack(null);
transaction.commit();
count++;
}
}
UPDATE:
In the link bellow, I got a representation of the present state of my application. When I enter the fragment for the second time, I want it to keep the accelerometer ON.
https://ibb.co/Sc7QZDt
I already tried the method popBackStack(), added a tag, name etc. and it doesn't work.
When I get out of the fragment the method getFragmentByTag() returns null.
This is the current state of the code:
public void onStartSensors(View view) {
FragmentManager fm = getSupportFragmentManager();
if(count > 0){
StartupFragment f1= (StartupFragment) fm.findFragmentByTag("A");
FragmentTransaction transaction=fm.beginTransaction();
if( f1 != null) {
transaction.attach(f1);
transaction.addToBackStack("sensors");
transaction.commit();
}
}
else{
StartupFragment f1=new StartupFragment();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.container, f1, "A");
transaction.addToBackStack("sensors");
transaction.commit();
count++;
}
}
It seems to me that you want to add a fragment to be displayed on top of the current activity that is able to be hidden and shown on demand. Looking at your code, every time you are 'showing' the fragment you are creating a new StartupFragment so the behaviour you are experiencing makes sense.
So I would instead recommend adding the fragment when the activity starts and then using FragmentTransaction.show(Fragment) and FragmentTransaction.hide(Fragment) to toggle it's visibility.

How to redirect user to a Fragment within an Activity from another activity

Activity A has 4 fragments.
Once user clicks on a link within the 4rth fragment a webview opens up.
The webview has a back button.
Once clicked it should take user to the previous 4rth fragment of Activity A.
Is there any documentation that can help me achieve that? I am a 5 month old Android developer any help on this would be great.
I do not need the code just need to point in the right direction.
do u mean activity A ,fragment 1->2->3->4 ->Activity 2 -> activity A fragment 1 ?
if so ,try this :
public void switchContent(Fragment fragment) {
if (fragment == null) {
return;
}
// replace fragment 4 to fragment 1
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content, fragment);
ft.commit();
}
ps. R.id.content is a frameLayout in Activity A

on popBackStack() which method invoked in previous fragment

I have Fragment A.
On clicking some button, i add Fragment B above Fragment A.
FragmentB fragmentB= new FragmentB ();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content_home, fragmentB, "fragmentB");
fragmentTransaction.hide(FragmentA.this);
fragmentTransaction.addToBackStack(FragmentA.class.getName());
fragmentTransaction.commit();
I perform some action in Fragment B. After finishing my work i popBackStack() current fragment.
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack();
Now i Fragment A. Since i used "ADD fragment" all my values are kept in edittext.
I want to know, which method is invoked when i come back to Fragment A,
is it onResume()..?
I have to put log in onResume, seems like it not going to onResume!
instead of fragmentTransaction.add(R.id.content_home, fragmentB, "fragmentB");
use
fragmentTransaction.replace(R.id.content_home, fragmentB, "fragmentB");
refer link Difference between add(), replace(), and addToBackStack()
try below code
FragmentB fragmentB= new FragmentB ();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_home, fragmentB, "fragmentB");
fragmentTransaction.hide(FragmentA.this);
fragmentTransaction.addToBackStack(FragmentA.class.getName());
fragmentTransaction.commit();
According to Fragment documentation, your fragment will be resumed after pressing back.
https://developer.android.com/guide/components/fragments.html
if you do call addToBackStack() when removing a fragment,
then the fragment is stopped and will be resumed if the user navigates
back.
But you said,
I have to put log in onResume, seems like it not going to onResume!
Then you may have some problems in your code:
- If you do
fragmentTransaction.hide(FragmentA.this);
then you may need to use FragmentTransaction.show(Fragment), in order to display FragmentA again. And then it will go to onResume().

Completely Refresh Fragment

I have a fragment and I need to completely refresh/reinstantiate it.
By refreshing I mean recreating fragment.
I have tried using FragmentManager with detach() method but it didn't help. All EditText children of that fragment still have their values entered even though the fragment was refreshed
Is there any way to achieve this result?
if there are only edit texts , create a method that sets all edit texts to empty string that is et(edit_text object),
et.setText(""); //edit text will be reset
Put the following code inside the fragment which need to be refreshed.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
Use fragment manger according to your code (if it is an old version then this will be supportFragmentManager)
Put and call this method to your fragment
private void reloadFragment(){
Fragment frg = getActivity().getSupportFragmentManager().findFragmentByTag(ReceiptFragment.TAG);
final FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}

Categories