Android: Switching Fragments and the Backstack - android

After struggling to make my simple fragment program work, I have not found any solution to the following: I manage two Fragments in my main activity: FragmentNeedle and FragmentPlot. Only one should appear at a time. The user has two bottons where he can select which fragment he wants to display. Also, when the FragmentPlot is showing, the user should be able to navigate back to the FragmentNeedle by pressing the back key. This is my Code:
public void onButtonPlotPressed()
{
FragmentManager manager = getSupportFragmentManager();
mPlotFragment = (PlotFragment) manager.findFragmentByTag(PlotFragment.class.getSimpleName());
if(mPlotFragment == null)
{
mPlotFragment = new PlotFragment();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.main_layout_center, mPlotFragment, PlotFragment.class.getSimpleName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
manager.executePendingTransactions();
}
public void OnButtonNeedlePressed()
{
FragmentManager manager = getSupportFragmentManager();
mFragmentNeedle = (FragmentNeedle) manager.findFragmentByTag(FragmentNeedle.class.getSimpleName());
FragmentTransaction ft = manager.beginTransaction();
if(mFragmentNeedle == null)
{
mFragmentNeedle= new FragmentNeedle();
ft.replace(R.id.main_layout_center, mFragmentNeedle, FragmentNeedle.class.getSimpleName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
manager.executePendingTransactions();
}
}
When the PlotFragment is shown, and I press back, I return to the NeedleFragment. But now when I try to change to Plot Fragment by pressing the UI button, it will keep showing the PlotFragment. If I remove the line ft.addToBackStack(), the switching between fragments works fine by pressing the buttons on the UI, but then I cannot go back with the back key. What am I doing something wrong?

you never add your FragmentNeedle to the backstack.
public void OnButtonNeedlePressed() {
mFragmentNeedle = (FragmentNeedle)manager.findFragmentByTag(FragmentNeedle.class.getSimpleName());
if(mFragmentNeedle == null) {
FragmentManager manager = getSupportFragmentManager();
mFragmentNeedle= new FragmentNeedle();
ft.replace(R.id.main_layout_center, mFragmentNeedle, FragmentNeedle.class.getSimpleName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//missing line
ft.addToBackStack(FragmentNeedle.class.getSimpleName());
ft.commit();
manager.executePendingTransactions();
}
}

Related

Not able to switch between fragment properly

I have 2 fragment on one fragment activity. I have 2 fragments say new and pending but I am having problem while switching from one fragment to another while tapping of button. When I am on pending tab and then click on new tab then sometimes it shows two loader and then it still shows pending page. My both fragments are android.support.v4.app.Fragment. I am attaching screenshot of the problem while I am tapping on new tab and still showing pending tab with two loader.
code for switching between fragment
#Override
public void onClick(View v) {
if (v.equals(neww))
{
Fragment newpage = new NewPageActivity();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.framelay, newpage);
ft.commit();
}
else if(v.equals(pending))
{
Fragment pendingFragment = new PendingPage();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.framelay, pendingFragment);
ft.commit();
}
}
now screenshot of problem
Try this:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.framelay, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();

Fragment title alone changes Screen remains same after navigation

I am navigating from one fragment to another fragment but my title alone changes my screen remains same. In the background the new fragment is up and running. This happens only when i navigate during any api call in the background.
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (main_fragment_container != null) {
ft.replace(main_fragment_container.getId(), fragment, fragment.getClass().getName());
}
if (isStackNeeded) {
ft.addToBackStack(fragment.getClass().getName());
}
ft.commit();

Adding fragments to backStack while moving forward and on clicking back navigating in hierarchy

I am new to Android programming.
I am working on a app in which I use Fragments, and I swap the fragments with
fragmentManager.beginTransaction().replace(R.id.frame_container,fragment)
.addToBackStack(null).commit();
I want to navigate back by clicking the device back button.
How can I do it?
Try doing this,I hope this will help
FragmentManager fm = getSupportFragmentManager();
String tag = f.getFragmentTag(); // instance method of a to get a tag
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, f, tag);
ft.addToBackStack(tag);
ft.commit();
}
#Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0)
{
fm.popBackStack();
} else
{
super.onBackPressed();
}
use some id instead of null in addToBackStack method.

Compare fragments

I've got the next code to switch between 4 fragments in a container [Main, A, B, C].
I need back button to go back to [Main] no matter how the user has navigated through fragments. For example, if I go [Main] >> [A] >> [C] when pressing back should go to [Main].
But I'm not getting the desired result. I think that I'm not doing well the coparisson between fragments.
Launcher MainFragment = new Launcher();
public void switchFragment(Fragment pFragment) {
FragmentManager fm = getSupportFragmentManager();
Fragment currentFragment = fm.findFragmentById(R.id.fragment_container);
if (pFragment == MainFragment){
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, pFragment).commit();
}
else if (currentFragment == MainFragment && pFragment != MainFragment){
//Fragment fr = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, pFragment).addToBackStack(null).commit();
}
else {
//Fragment fr = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, pFragment).commit();
}
currentFragment = pFragment;
}
UPDATE--
I've seen this way is working better, but still makes issue. If I navigate through some fragments without returning back to the Main fragment, when I press back it doesn't go back. It's like if there was some issue with the popbackstack().
public void switchFragment(Fragment pFragment) {
FragmentManager fm = getSupportFragmentManager();
Fragment currentFragment = fm.findFragmentById(R.id.fragment_container);
if (pFragment.equals(MainFragment)){
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, pFragment).commit();
}
else if (currentFragment.equals(MainFragment) && !pFragment.equals(MainFragment)){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, pFragment).addToBackStack(null).commit();
}
else {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, pFragment).commit();
}
}
This functionality can be achieved by removing .addToBackStack from your fragment transactions.
Here is the Android reference for back stack functionality within fragments: http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments
I finally get the dessired result just replacing the fragment I want on the back_key onClickListener:
final OnClickListener goBack = new OnClickListener() {
#Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, MainFragment).commit();
}
};
This way, if In the future I need to go back to another fragment instead of the Main, I'll just add a condition to compare fragment tags and do something depending the tag.

Show and Hide Fragments on Button Click

I'm developing an application which consists of Fragments, When I click on the button it should show and hide. By default I have set fragments as visibility gone, even though this is also not working for me:
FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
mMenuFragment = new MenuFragment();
mFragmentTransaction.add(R.id.frmMenuBar, mMenuFragment);
mFragmentTransaction.hide(mMenuFragment);
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.commit();
The code below I kept on button click.
#Override
protected void onMenuButtonClick(View Button) {
super.onMenuButtonClick(Button);
System.out.println("Botton Clciked");
if (Button.isClickable()) {
FragmentTransaction menuTransaction = getFragmentManager().beginTransaction();
menuTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
menuTransaction.show(mMenuFragment);
menuTransaction.addToBackStack(null);
menuTransaction.commit();
} else {
FragmentTransaction menuShowTransaction = getFragmentManager().beginTransaction();
menuShowTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
menuShowTransaction.hide(mMenuFragment);
menuShowTransaction.addToBackStack(null);
menuShowTransaction.commit();
}
}
Please can any one help me.

Categories

Resources