This is my Activity class:
private void pushFragment(Fragment fragment)
{
if (fragment == null)
return;
FragmentManager fragmentManager = SupportFragmentManager;
if (fragmentManager != null)
{
FragmentTransaction ft = fragmentManager.BeginTransaction();
if (ft != null)
{
ft.AddToBackStack("");
ft.Replace(Resource.Id.rootLayout, fragment);
ft.Commit();
}
}
}
public override void OnBackPressed()
{
if (FragmentManager.BackStackEntryCount > 0)
{
FragmentManager.PopBackStack();
//Finish();
}
else
{
base.OnBackPressed();
}
}
When I click on the close icon of the layout it will call pushFragment(new ParentFragment());
The close button on top of the right, when clicked must go to fragment but it's not working?
Related
I am using bottom navigation drawer to switch between fragments, the problem is that every time I switch back to a fragment it gets recreated.
How can I save the state of fragment and resume it when switched back to it?
bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
#Override
public void onTabSelected(int position) {
if (position==0){
loadFragment(new Daily());
}
if (position==1){
loadFragment(new Trending());
}
if (position==2){
loadFragment(new Random());
}
}
#Override
public void onTabUnselected(int position) {
}
#Override
public void onTabReselected(int position) {
}
});
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.attach( fragment);
transaction.addToBackStack(null);
transaction.commit();
}
The issue is that you are always creating a new Fragment on any onTabSelected. So in order to fix it, you need to work with your FragmentManager.
Possible solution: use the add and show/hide methods.
Example:
private static final String DAILY_TAG = BuildConfig.APPLICATION_ID + ".DAILY_TAG";
private static final String TRENDING_TAG = BuildConfig.APPLICATION_ID + ".TRENDING_TAG";
private static final String RANDOM_TAG = BuildConfig.APPLICATION_ID + ".RANDOM_TAG";
public void onTabSelected(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();
if (position == 0) {
hideFragment(TRENDING_TAG)
hideFragment(RANDOM_TAG)
Fragment fragment = fragmentManager.findFragmentByTag(DAILY_TAG);
FragmentTransaction transaction = fragmentManager.beginTransaction()
if (fragment != null) {
transaction.show(fragment)
} else {
transaction.add(content.id, new Daily(), DAILY_TAG)
}
transaction.commitNow()
} else if (position == 1) {
hideFragment(DAILY_TAG)
hideFragment(RANDOM_TAG)
Fragment fragment = fragmentManager.findFragmentByTag(TRENDING_TAG);
FragmentTransaction transaction = fragmentManager.beginTransaction()
if (fragment != null) {
transaction.show(fragment)
} else {
transaction.add(content.id, new Trending(), TRENDING_TAG)
}
transaction.commitNow()
} else {
hideFragment(TRENDING_TAG)
hideFragment(DAILY_TAG)
Fragment fragment = fragmentManager.findFragmentByTag(RANDOM_TAG);
FragmentTransaction transaction = fragmentManager.beginTransaction()
if (fragment != null) {
transaction.show(fragment)
} else {
transaction.add(content.id, new Random(), RANDOM_TAG)
}
transaction.commitNow()
}
fragments.put(position, fragment);
loadFragment(fragment);
}
private void hideFragment(String tag) {
FragmentManager fragmentManager = getSupportFragmentManager()
Fragment currentFragment = fragmentManager.findFragmentByTag(tag)
if (currentFragment != null) {
fragmentManager.beginTransaction().hide(currentFragment).commitNow()
}
}
PS - The code can be optimized.
How can I bring fragment to top of other fragments ?
I added fragment like bellow :
#OnClick(R.id.imbEvents)
void clickEvents() {
String tagName = returnStatusFragment(getString(R.string.events_fragment));
if (!TextUtils.isEmpty(tagName) && tagName.equals(getString(R.string.events_fragment))) {
//Fragment is opened
} else {
if (getActivity() != null) {
frameWorkTableFragment.setVisibility(View.VISIBLE);
EventsFragment eventsFragment = new EventsFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_work_table_fragment,
eventsFragment,
getString(R.string.events_fragment))
.addToBackStack(getString(R.string.events_fragment))
.commit();
}
}
}
I used from bellow code to bring fragment to top but close all last fragments:
getActivity().getSupportFragmentManager().popBackStack(tagName, 0);
Method of returnStatusFragment is :
private String returnStatusFragment(String tagName) {
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
Fragment tempFragment = fragmentManager.findFragmentByTag(tagName);
if (tempFragment != null) {
return tempFragment.getTag();
} else {
return "";
}
}
My doBackTabZero:
#Override
public void doBackTabZero(String nameFragment) {
android.support.v4.app.FragmentManager fragmentManagers = getActivity().getSupportFragmentManager();
Log.i("ASDSADASDSASD", "3 " + nameFragment + " " + fragmentManagers.getBackStackEntryCount());
if (fragmentManagers.getBackStackEntryCount() > 1) {
fragmentManagers.popBackStackImmediate();
} else {
if (nameFragment.equals("EventsFragment")) {
BusDisplayStatusFrameLayout busDisplayStatusFrameLayout = new BusDisplayStatusFrameLayout();
busDisplayStatusFrameLayout.setDisplayStatusFrameLayout(1);
EventBus.getDefault().post(busDisplayStatusFrameLayout);
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentManager.popBackStackImmediate();
fragmentTransaction.remove(EventsFragment.this);
fragmentTransaction.commit();
}
}
}
replace this
getActivity().getSupportFragmentManager().popBackStack(tagName, 0);
with
Fragment eventsFragment = fragmentManager.findFragmentByTag(getString(R.string.events_fragment));
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_work_table_fragment,
eventsFragment,
getString(R.string.events_fragment)).commit();//don't add to back stack again
onBackPressed
#Override
public void doBackTabZero(String nameFragment) {
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
if(fragmentManager.getBackStackEntryCount() > 1 ){
fragmentManager.popBackStackImmediate();
}else{
super.onBackPressed();
}
}
I am working on single activity based principle. But I am facing a problem when the same fragment is open again because its again adds in fragment backstack entry. So backstack contains multiple backstack entries for same fragment. This creates problem on back navigation.
Example :- A|B|C|D|A|C|A
So when I press back key same fragment is displaying multiple times. Is there any way to reuse the existing fragment from backstack entry.
I am managing my backstack like this :-
fragmentManager.beginTransaction().setCustomAnimations(R.anim.fragment_enter,
R.anim.fragment_exit, R.anim.pop_enter, R.anim.pop_exit).
add(R.id.frameLayout, fragment).addToBackStack(backStateName).commit();
Any kind of help will be appreciated.
private void createOrResumeFragment(String fragmentTag){
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
boolean fragmentPopped = manager.popBackStackImmediate (fragmentTag, 0);
Fragment fragment = manager.findFragmentByTag(fragmentTag);
if(!fragmentPopped && fragment == null){
//Create an new instance if it is null and add it to stack
fragment = new MyFragment();
ft.addToBackStack(fragmentTag);
}
ft.replace(R.id.framelayout, fragment);
ft.commit();
}
Trying this using the fragment list
See the Answer Here
Initialize the fragments list
static List<String> fragments = new ArrayList<String>();
on Start of first fragment on Activity add this
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.frame, new AFragment()).commit();
fragments.add("package.fragment.AFragment");
}
Code to fragment change and take in back stack
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
//fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
if(!fragments.contains(backStateName)) {
// ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
ft.replace(R.id.frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
fragments.add(backStateName);
System.out.println("backStateName" + fragments);
}
else
{
ft.replace(R.id.frame, fragment);
ft.commit();
}
onBackpressed
#Override
public void onBackPressed() {
if(fragments.size()>0)
{
fragments.remove(fragments.size()-1);
}
super.onBackPressed();
}
for back remove stack
final android.app.FragmentManager fm = getFragmentManager();
fm.addOnBackStackChangedListener(new android.app.FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() ==0) {
// dLayout.closeDrawers();
finish();
}
else
{
// dLayout.closeDrawers();
}
}
});
Before adding or replacing the fragment on backstack, check that if the fragment already in backstack or not.
boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
if (fragmentPopped) {
// fragment is popped from backStack
} else {
// add or replace your fragment here
}
public void changeFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment currentVisibleFragment = fragmentManager.findFragmentById(R.id.container);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment, fragment.getClass().getSimpleName());
if (!currentVisibleFragment.getClass().getSimpleName().trim().equalsIgnoreCase(fragment.getClass().getSimpleName().trim())) {
for (int i = fragmentManager.getBackStackEntryCount() - 1; i > 0; i--) {
if (fragmentManager.getBackStackEntryAt(i).getName().equalsIgnoreCase(fragment.getClass().getSimpleName())) {
fragmentManager.popBackStack(fragmentManager.getBackStackEntryAt(i).getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
} else {
fragmentManager.popBackStack();
fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
}
fragmentTransaction.commit();
}
boolean doubleBackToExitPressedOnce = false;
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
super.onBackPressed();
} else {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
finish();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Are you sure you want to exit?", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
}
Call the method to replace fragment with single entry in backstack
changeFragment(new YourFragmentClassName());
So here is my navigationDrawer in image below has menu items ,and every single item add a fragment and load it own data , so how to prevent re-added a fragment once it's loaded, it's re-added the fragment and i don't want that.
here is what i tried :
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
if (menuItem.getItemId() == R.id.nav_home) {
makeTransaction(new Home(), "home_fragment");
toolbarTitle.setText("Home");
} else if (menuItem.getItemId() == R.id.nav_prizes) {
makeTransaction(new Prizes(), "prizes_fragment");
toolbarTitle.setText("Prizes");
} else if (menuItem.getItemId() == R.id.nav_myAccount) {
mDrawerLayout.closeDrawer(GravityCompat.START);
startActivity(new Intent(MainActivity.this, MyAccount.class));
} else if (menuItem.getItemId() == R.id.nav_contact_us) {
makeTransaction(new ContactUs(), "contactUs_fragment");
toolbarTitle.setText("Contact Us");
} else if (menuItem.getItemId() == R.id.nav_about_us) {
makeTransaction(new AboutUs(), "aboutUs_fragment");
toolbarTitle.setText("About Us");
} else if (menuItem.getItemId() == R.id.nav_share) {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
return false;
}
public void makeTransaction(Fragment fragment, String tag) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment test = getFragmentManager().findFragmentByTag(tag);
getFragmentManager().executePendingTransactions();
if (test != null && test.isVisible()) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else if (test != null && test.isAdded()) {
getFragmentManager().popBackStack(tag,0);
} else {
mDrawerLayout.closeDrawer(GravityCompat.START);
transaction.setCustomAnimations(R.anim.enter_from_left,
R.anim.exit_to_right, 0, 0);
transaction.add(R.id.fragment_container, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
}
}
I face similar problem that on clicking same item again and again it reload same fragment every time
For this you can make method
public boolean checkFragment(int position, Fragment mFragment) {
if ((mFragment instanceof "your_fragment_name"))
return true;
return false;
}
and before committing fragment
you can add this
if (checkFragment(position, mFragment)) return;
As
if (checkFragment(position, mFragment)) return;
transaction.setCustomAnimations(R.anim.enter_from_left,
R.anim.exit_to_right, 0, 0);
transaction.replace(R.id.fragment_container, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
EDIT
Please replace your makeTransaction method with below code.
public void makeTransaction(Fragment fragment, String tag) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment test = getFragmentManager().findFragmentByTag(tag);
if (test != null && test.isVisible()) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else if (test != null) {
getFragmentManager().popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
// getSupportFragmentManager().popBackStackImmediate(tag, 0);
} else {
mDrawerLayout.closeDrawer(GravityCompat.START);
if (checkFragment(position, mFragment)) return;
transaction.setCustomAnimations(R.anim.enter_from_left,
R.anim.exit_to_right, 0, 0);
transaction.replace(R.id.fragment_container, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
}
}
If source is an object variable, then instanceof is a way of checking to see if it is a Fragment or not.
So we use this property to check that if currently clicked item is instance of its fragment or not.
Declare variable for FragmentManager outside of the methods:
FragmentManager fm;
Inside somewhere in onCreate(), you initialize it.
fm = getFragmentManager();
In your makeTransaction() use fm instead of "getFragmentManager()"
FragmentTransaction transaction = fm.beginTransaction();
and etc..
This make your management easier, and you can find fragments using TAG like that:
Fragment fragment = ({FRAGMENT_CLASS}) fm.findFragmentByTag({TAG});
if (fragment == null) {
//add to transaction
} else {
//use existing thorugh fragment
}
If after that the fragment isn't null it'll have the link to the fragment which is created using that TAG.
if you create following fragment:
makeTransaction(new ContactUs(), "contactUs_fragment");
You would be able to get reference to it like this:
Fragment fragment = (ContactUs) fm.findFragmentByTag("contactUs_fragment");
and do with it anything you want.
I am using a navigation drawer.So when my application starts i am calling the homeFragment and keeping it in the backstack. Now if the user selects any options from the navigation drawer i am opening the respective fragment but without adding them to backstack. So what i want is that even when user has open 10 fragment, on pressing back they should be taken back to homeFragment only. But with my code the app exits on pressing back even homeFragment is in the backstack.
Code to openFragments
public static void replaceFragment(FragmentActivity activity, Fragment fragment, boolean addToBackStack) {
try {
String backStateName = fragment.getClass().getName();
String fragmentTag = backStateName;
FragmentManager manager = activity.getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate(fragmentTag, 0);
if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) {
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.frag_container, fragment, fragmentTag);
if (addToBackStack) {
ft.addToBackStack(backStateName);
}
ft.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
}
From MainActivity
private void init() {
setUpToolBar();
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerFrag = (NavigationDrawerFrag) getSupportFragmentManager().findFragmentById(R.id.frag_nav_drawer);
drawerFrag.setUp(drawerLayout, toolbar, R.id.frag_nav_drawer);
CommonFunctions.replaceFragment(this, new HomeFrag(), true);
}
On NavigationDrawer Item click
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 1) {
CommonFunctions.replaceFragment(getActivity(), new ProfileFrag(), false);
} else if (position == 2) {
CommonFunctions.replaceFragment(getActivity(), new CelebrityFrag(), false);
} else if (position == 4) {
CommonFunctions.replaceFragment(getActivity(), new AboutUsFrag(), false);
} else if (position == 5) {
CommonFunctions.replaceFragment(getActivity(), new TermsAndConditions(), false);
}
lvNavItems.setItemChecked(position, true);
//lvNavItems.setSelection(position);
lvNavItems.setSelected(true);
mDrawerLayout.closeDrawer(fragContainer);
}
Add onBackPressed() method in activity to check backstack and if entry is more then zero so call popbackstack method.
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
//Put this in your onNavigationItemSelected of MainActivity
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//FrameLayout where you load your home fragment tools:layout="#layout/frag_home"
ft.replace(R.id.content_frame,nav_selected);
if(getSupportFragmentManager().getBackStackEntryCount() != 0){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
getSupportFragmentManager().popBackStack();
}
//Don't add Home screen
if(id != R.id.nav_home){
ft.addToBackStack(null);
}
ft.commit();