OnBackPressed for each item in the NavigatorView - java

There is a NavigatorView in which I am using the NavController. The whole problem is that when replacing the Fragmenta, I immediately set fragmentTransaction.addToBackStack (String.valueOf (FragmentRink3)); and when navigating to another NavController when clicking on OnBackPressed, is applied to the fragment from the previous NavController where the replacement was made. It's hard to explain. Is it possible to somehow do so that each of the lower navigation menu has its own, I don’t know, a list of clicks, according to which the sequence of transitions to fragments was determined? I know I explained very poorly, I will attach a gif to show the problem clearly.
As you can see, by clicking on the button, the fragment was replaced, then I went to the next menu and clicked OnBackPressed, and at this moment the fragment, which was replaced in the previous menu, went back. How to fix it?
I will replace the fragment as follows:
btn_close.setOnClickListener(v ->
{
FragmentRink4 fragment = new FragmentRink4(); // you fragment
FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.nav_controller_rink1, fragment);// your container and your fragment
fragmentTransaction.addToBackStack(String.valueOf(FragmentRink3));
fragmentTransaction.commit();
});
Here is a link to the project.

Related

Navigation View button on all screens and refresh home

I'm creating an application to which I've added a Botton Navigation View but I don't know if there is any way that it appears in all the Activity or if on the contrary I would have to specify it in each of them to appear, as well as adding all its functionality on each screen, since this last option I see a little cumbersome.
The other question is for the button Home of this bar, I have added a button that is home to return to the main Activity each time I pulse, in the way that I have done has been with an Intent that goes to that screen itself, but I do not know if there is another method:
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.btnHome:
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
break;
Check out how to use Fragment in Android. You don't have to open a new activity each time the button in your BottomNavigationView is clicked, instead, you use fragments, so only the selected part in activity is changed. Check out the following tutorial to learn more about Fragments. See the "dynamic creation" of fragments.
How to use Fragments in Android.
In your case, there'll be code of FragmentTransaction where you're launching a new Activity using Intent. You'll be replacing only a certain portion of the screen(between your Toolbar and `BottomNavigationView).

refresh a fragment in a tab in android

I have one tab, where the user puts in some Information and can read them in the other. Therefore I need to refresh the second tab, but I have no clue how.
I already tried this code:
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
After the user saves some data, but I get a nullpointerException within the FragmentManager, when I use the fragment tag, I defined in the xml document.

Navigation drawer - top fragment

I have Navigation drawer and a series of fragments. On last fragment of hierarchy, i've got UP button on action bar. As this button clicked, onOptionsItemSelected(MenuItem item) of root activity is called, where i can find out, which button is clicked in the following predicate :
else if ( item.getItemId() == android.R.id.home )
{
System.out.println("android.R.id.home\n\n");
// super.onBackPressed();
// getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
return true;
}
How can i remove current fragment? I've read about back stack, but that approach requires saving fragments in stack using keys. How can i avoid this procedure?
I had this problem before of fragments on top of each other when I first started using them. I use this code to add fragments:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction =fragmentManager.beginTransaction();
Fragment fragment = new CustomFragment(); // your fragment to add
transaction.replace(R.id.content_frame, fragment,CustomFragment.TAG); // a string in case you need to check with fragment is currently visible.
transaction.addToBackStack(null);
transaction.commit();
This will ensure that when you press back button the current one will disappear and the one before it will show.
hope it works with you.
You can actually just pop off the back stack, no tracking via keys/tags needed: FragmentManager.popBackStack(). Just use null when adding your fragments to the back stack, the system will automatically select a tag for it.

Keeping the back stack after it is being cleared by opening a new activity

In the main activity of my app there is a container that hosts fragments.
When a user clicks a button in the "default" fragment (the first fragment that is displayed), the fragment changes, and so do the actionbar buttons.
One of the buttons in the actionbar of this new fragment open another activity.
In that activity, when a user clicks the back button, the activity closes, and the fragment that was shown in the MainActivity (the fragment that opened the new activity) is still there (which is fine).
However, if a user clicks the back button again, it does not return to the previous fragment. While it does return when the activity does not open.
It turns out that opening the activity clears the backstack (verified by Logging the count from the FragmentManager class), while I'm not quite sure whether this is supposed to behave like this or not, it kinda makes sense. Unfortunately, it is not the behavior I desire.
MainActivity: Fragment A (default) ---> Fragment B ---> Acivity B
Therefore, my question is how can I keep the backstack after the activity resumes, if at all?
I tried searching for similar questions, but all questions I found actually asked how to clear the backstack.
Try that:
#Override
public void onBackPressed() {
Intent intent = new Intent(A_Acticity.this, B_Activity.class);
startActivity(intent);
}
Hope it helped! :)
Reading the documentation, there is a way to pop the back stack based on either the transaction name or the id provided by commit. Using the name may be easier since it shouldn't require keeping track of a number that may change and reinforces the "unique back stack entry" logic.
Since you want only one back stack entry per Fragment, make the back state name the Fragment's class name (via getClass().getName()). Then when replacing a Fragment, use the popBackStackImmediate() method. If it returns true, it means there is an instance of the Fragment in the back stack. If not, actually execute the Fragment replacement logic.
private void replaceFragment (Fragment fragment){
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
}
}

How can I use an OnClickListener inside of a fragment to swap between the other fragments

I have an Activity that displays 2 fragments. One acts as a list of options and includes a few textviews and the other is the corresponding fragment. What is the best way use OnClick for those text views to swap between the corresponding fragments?
Thank you in advanced, I really appreciate it.
this is the basic code for swapping fragments:
import android.support.v4.app.Fragment;
//If not using "support" fragment, use getFragmentManager()
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//you can omit this, Android.R. has some built in anmations.
ft.setCustomAnimations(android.R.anim.slide_in_left, R.animator.fade_out);
//"replace" empties the container of all existing fragments.
ft.replace(container, myFrag, str_SomeID);
ft.commit();
Alternatively you can hide a fragment in place.
The basic code for hiding a fragment:
FragmentManager manager = getSupportFragmentManager();
fragment = manager.findFragmentByTag(str_SomeID);
if (fragment != null && !fragment.isHidden()){
manager.beginTransaction().hide(fragment).commit();
}

Categories