I've been working on a project that has swipeable views with tabs.
The tabs exist as fragments. I want to run a fragment method from the main activity on a periodic basis.
To run the methos, I dont' have the ID for the fragment. I read in other posts that:
String tag = "android:switcher:" + TabActivity.this.viewPager.getId() + ":" + "0";
Tab1Fragment fragment = (Tab1Fragment) getSupportFragmentManager().findFragmentByTag(tag);
fragment.someMethod();
will return the fragment I desire, which happens to be the zero-th or first tab. However, running this code returns a nullpointer exception.
Any help will be appreciated. Thanks :)
In your code, at some point you must have:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.framelayout, new SomeXYZFragment(), "some_xyz_fragment");
ft.commit();
Add the line:
fm.executePendingTransactions();
after the line ft.commit();
Try this. It should work.
Related
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.
i have below code for data remove from DB.
So i delete data but i want to update fragment from here.
how i can refesh fragment ?
// Remove entry from database table
database.delete(FAVORITES_TABLE, FAVORITES_ITEM_ID + " = " + soundObject.getItemID(), null);
// Refesh current fragment
????????????????????????????????
this is code which i use in Fragment to refesh it (on button press) work good, but i don't want to tap refesh button to refesh page. I want fragment refesh when DB change by it self.
public void refersh()
{
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
}
any idea?
Thanks
you are detaching and attaching again in one commit, so it does nothing. make it two commits
public void refersh(){
FragmentTransaction ft1 = getFragmentManager().beginTransaction();
ft1.detach(this).commit();
FragmentTransaction ft2 = getFragmentManager().beginTransaction();
ft2.attach(this).commit();
}
there is also possibility that you shoud use getSupportFragmentManager instead of getFragmentManager
its probably not best/most-efficient solution, but you posted ony this small snippet...
In simple terms you can use:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
I think you want to refresh the fragment contents upon DB data updates
If so, detach the fragment and again attach it
// Reload current fragment
Fragment f1 = null;
f1 = getSupportFragmentManager().findFragmentByTag("Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(f1);
ft.attach(frg);
ft.commit();
Fragment_TAG is the name you gave your fragment when you created it
To refresh the current fragment content, you can use the following code inside the method refresh()
public void refresh()
{
Fragment fragment = null;
fragment = new Current_Fragment_Name(); //replace the Current_Fragment_Name with your current fragment name
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
Hope it works for you.
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
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().
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();
}