PopBackStack previous last fragment and check which fragment it is - java

I got a few fragments in MainActivity which can be selected from Navigation Drawer. Now, whenever users press a profile button, it will jumps to UserProfile Fragment. If home button is pressed, it will pop back the last fragment. Since I've assigned each of the fragments a specific backstack name i.e .addToBackStack("abc"), how can I check what is the last fragment by using popBackStack() method ?

To get the last fragment:
FragmentManager fm = getSupportFragmentManager();
int lastFragEntry = fm.getBackStackEntryCount()-1;
String lastFragTag = fm.getBackStackEntryAt(lastFragEntry).getName();
Log.i("Last Fragment Tag->", lastFragTag);
NB: If you want to get the name/tag of last fragment, you also have to use the same Tag during fragment transaction:
ft.replace(android.R.id.container, fragment, "abc");
ft.addToBackStack("abc");
Hope this helps.

Related

how Set OptionsMenu when certain fragment is shown

In my main Activity, I have a fragmentContainerView in which I am changing the fragments by using bottomNavigationbar which has 2 navigation Items (home & profile).
Now I want to show OptionsMenu the "profile" fragment is shown and then again remove it when the "home" fragment is shown.
How can I do it? or is there an alternative?
Create a public method in the activity to change set the option menu and call it from the fragment like below.
HomeActivity homeActivity=(HomeActivity) getActivity();
assert homeActivity!=null;
homeActivity.yourPublicMethod();

Call Main activity from a Fragment

fragment class
Home/main activity
How to limit my main activity to not go back on the last page of my Fragment once return from the last page of Fragment to Main activity like I have One Home activity and that home activity leads to 3 more activities on clicking the button and in which one activity has fragments. The last page of the fragment is leading to home activity but when I back pressed on the home activity it again leads me to the last page of a fragment from where I've come and then I press the back button again then my application shuts
When you are adding your Fragments to the FragmentManager make sure to avoid adding it to the backstack, if you don't want it to appear on its own.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_layout,fragment);
//Adding null will stop the fragment from reappearing on a backpress.
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
In the future please post code in text format. Do not post images of code. If you need to show some view behavior on the screen then its fine to post images, but not code.

Android: add two or more fragments to the same backstack state

I have a main activity with a RelativeLayout container where i load different fragments using theFragmentManager.
Let's say i load fragment1 and fragment2 with two transactions. In case of back button pressed, i want to return to the state when both fragment1 and fragment2 were not loaded. Is this possible?
I've tried to use the same name in the addToBackStack() method:
mFragmentManager.beginTransaction().replace(R.id.activity_main, fragment1).addToBackStack("SameState").commit();
mFragmentManager.beginTransaction().add(R.id.activity_main, fragment2).addToBackStack("SameState").commit();
but this doesn't work. If i press the back button fragment2 disappears first, then pressing again fragment1 goes away too. Any suggestion?
Try this:
mFragmentManager.beginTransaction()
.replace(R.id.activity_main,fragment1)
.addToBackStack(null)
.commit();
getSupportFragmentManager().popBackStack(); // Pop the first transaction
mFragmentManager.beginTransaction()
.replace(R.id.activity_main,fragment2)
.addToBackStack(null)
.commit();
At this point, when you press the back button, it will simply revert the last transaction (fragment2 replacing).

Unable to start a fragment from a fragment

Hello everyone i have a view pager named "viewpager" and two fragments named "Add_Create" and "Add_Create_2".I am using Tab layout to show different tabs.Now in one of the tabs there is "Add_Create" fragment.In Add_Create fragment there is a button and on that button click i want to show Add_Create_2 fragment.Below is my code that i have tried
Add_Create_2 add= new Add_Create_2();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.viewpager,add);
transaction.addToBackStack(null);
transaction.commit();
Now this code is present in first fragment named "Add_Create".
The problem could be at the viewPager. What kind of element is this? If it is a ViewPager, you cannot load it a Fragment. You should to create on it maybe a RelativeLayout, and it will be your fragment container.
Instead of using getFragmentManager(), use getSupportFragmentManager(). Since you're calling it from a fragment, maybe getActivity().getSupportFragmentManager()

Android from Fragment to Activity, back press dont go back to fragemt

I am loading an activity from my fragment. If I press the back button on the device, it goes back to the Gridfragment (as expected).
But, if I click the action bar back arrow, it goes back to the mainActivity hosted fragment = HomeFragment and not the Gridfragment.
From HomeFragment - ( default fragment)
Fragment f = GridFragment.newInstance(GridFragment.TAG_bundlePhotosList);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).addToBackStack(null).commit();
Fragment Grid:
Intent i = new Intent(getActivity(), ImagePreviewActivity.class);
i.putExtra("from_grid", "from_grid");
tartActivity(i);
ImagePreviewActivity :
//Display back arrow
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(ImagePreviewActivity);
This is the same problem, and as such the same solution as here.
When you use UP navigation, then the previous activity is recreated. To prevent that from happening while you preserve the UP navigation, you can get the intent of the parent activity, and bring it to front if it exists, otherwise create it if not.

Categories

Resources