I have one SplashActivity and one MainActivity and all the fragments are associated with MainActivity. Now whenever I recive Notification from server I want to open one specific fragment. I am opening this like
final android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if(getIntent().getAction().equals("OpenNotificationFragment")) {
fragmentManager.beginTransaction()
.replace(R.id.fragment_main_container, new NotificationFragment()).addToBackStack(PastOrderFragment.TAG)
.commitAllowingStateLoss();
}
but during my splash image is loading it is throwing NullPointerException, should I call this method in SplashActivity or edit some of my code.
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 a Recycler View in a Fragment that I have an adapter class for in a separate file. I am trying to make it so that when you click on a RecyclerView Item it replaces the current fragment with a new one. The code to replace a fragment with another that I had been using previously is this:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, NewFragment.class, null);
transaction.addToBackStack("CurrentFragment");
transaction.commit();
And this works inside the fragment, but since I have to call it from the java class I tried this instead:
FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, NewFragment.class, null);
transaction.addToBackStack("CurrentFragment");
transaction.commit();
And this just layers the new fragment on top of the current one. If I try making a class in the Current Fragment with the transaction code in it and call it, it doesn't work because of the static context.
Is there any way to do this?
I am trying to create a custom class to handle change fragment. However, I cannot get the fragment manager in order to execute change fragment. What I have tried is to extend the class to FragmentActivity and get the fragment manager by using getSupportFragmentManager(). However, it returns error with Fragment manager not attached to host
Any idea that I can get the correct fragment manager to execute changing fragment? should I extend to another class?
public class changefragment extends FragmentActivity{
public void ChangeFragment(){
Fragment f = new MyCustomFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.nav_host_fragment,f);
ft.addToBackStack(null);
ft.commit;
}
}
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().