Compare fragments - android

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.

Related

Android find current fragment

I have 4 java class that i use as fragments: `F1,F2,F3,F4.
When i want to swich from one to other, i use this code:
android.app.FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
F1 FP = new F1();
FT.add(R.id.where, FP,"F1");
FT.commit();
FM.executePendingTransactions();
If i want to make possible to return from one of them to the previous, i add addToBackStack(TAG) as following:
android.app.FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
F1 FP = new F1();
FT.add(R.id.where, FP,"F1");
FT.addToBackStack("F1");
FT.commit();
FM.executePendingTransactions();
I have only one activity that chages the current fragment displayed.
I would get from it, the current fragment displayed.
I tried to write this, but now is always null.
Fragment now=getSupportFragmentManager().findFragmentByTag("F1");
if(now!=null && now.isVisible()) {
//some code for the current fragment
}
Try This it may be help to you
Fragment currentFragment = getFragmentManager().findFragmentById(R.id.where);
if (currentFragment instanceof F1) {
//do your stuff here
}

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.

setCustomAnimations not working on back button press

I've got this issue where transitions from fragment A to B working fine but on backPress they do not. Looking around I saw many with this issue but none of the answers seems to help me out. I must be doing something wrong but have no idea what and I would love some help, it drives me nuts!
This is my logic:
Fragment A calling fragment B:
private void loadNextFragment(WeatherComplete[] weatherDataArrayList) {
FragmentManager fm = getFragmentManager();
MainFragment mf = MainFragment.newInstance();
mf.setVars(choosenCity, weatherDataArrayList);
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in, R.anim.slide_iout, R.anim.slide_in, R.anim.slide_iout);
ft.addToBackStack(null);
ft.replace(R.id.frame, mf);
ft.commit();
}
Fragment B calling fragment C (Settings) from menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
final FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in, R.anim.slide_iout, R.anim.slide_in, R.anim.slide_iout);
ft.replace(R.id.frame, new Settings(), "settings");
ft.commit();
break;
I'm passing an object array from fragment A to fragment B, thus when clicking the back button on fragment C it will go back to fragment B but there is no object array to work with so in that case I want fragment C to go back to fragment A instead - I'm popping the backStack:
if (weatherDataObj == null) {
Log.d(TAG, "WEATHER DATA IS NULL");
FragmentTransaction ft = getFragmentManager().beginTransaction();
getFragmentManager().popBackStack();
// ft.remove(this);
ft.commit();
}
Try using this:
ft.setCustomAnimations(R.anim.slide_in, R.anim.slide_iout,
R.anim.slide_in, R.anim.slide_iout);

How to avoid duplicate fragments?

I have a button in my fragment (fragment_x) with the below OnClickListener:
private void onClickAddButton(View view){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment_y fragment_y = new Fragment_y();
fragmentTransaction.add(R.id.rl_activity_main_container, fragment_x);
fragmentTransaction.commit();
}
The problem is that this button is always visible, so clicking it again will add one more fragment_y and the screen gets messed up. How how do I check whether fragment_y has already been added, so that I can avoid creating a duplicate fragment_y?
You can ask the FragmentManager if the Fragment is already added:
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentByTag(tag);
if (fragment == null) {
// fragment must be added
fragment = new Fragment();
fm.beginTransaction().add(R.id.container, fragment, tag);
} else {
// fragment already added
});

Android: Switching Fragments and the Backstack

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();
}
}

Categories

Resources