retain state in navigation drawer fragments - android

i have a navigation drawer project which I just modify from the "Create New Project > Navigation Drawer" using Android Studio. There are several 'menus' in the navigation drawer, on each opens up different fragments.. the problem is every time when I navigate using the drawer, the fragments are re-created. Means that when the user have done some changes on a fragment, later when the user re-visits the fragment again, the changes are lost. I've used setRetainInstance(true); but that only works for orientation change it seems.
here's a part of my code on which I use to display the selected fragments:
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
Fragment fragment = null;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
FragmentManager fm = getFragmentManager();
switch (position) {
case 0:
fragment = fm.findFragmentByTag("fragmentDashboard");
if (fragment == null){
fragment = new Dashboard();
transaction.replace(R.id.container, fragment, "fragmentDashboard");
transaction.addToBackStack(null);
transaction.commit();
}else{
transaction.remove(getFragmentManager().findFragmentByTag("fragmentDashboard"));
transaction.commit();
fm.popBackStack();
}
break;
case 1:
fragment = fm.findFragmentByTag("fragmentExpenses");
if (fragment == null) {
fragment = new Expenses();
transaction.replace(R.id.container, fragment, "fragmentExpenses");
transaction.addToBackStack(null);
transaction.commit();
}else{
transaction.remove(getFragmentManager().findFragmentByTag("fragmentExpenses"));
transaction.commit();
fm.popBackStack();
}
break;
default:
break;
}
}
edit: this question Save fragment state with navigation drawer is the close solution I get to my problem, but the answer is too brief.. and I'm pretty new in android development. so can anyone help?

Try this Code :: Instead of replacing-fragments .... EveryTime Hide and Show the fragments
Description::
Instead of destroying the fragments .... create the fragment for
the first time and every other time find the fragment and use it,
instead of replacing it
Instead of code ::
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
Fragment fragment = null;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
FragmentManager fm = getFragmentManager();
switch (position) {
case 0:
fragment = fm.findFragmentByTag("fragmentDashboard");
if (fragment == null){
fragment = new Dashboard();
transaction.replace(R.id.container, fragment, "fragmentDashboard");
transaction.addToBackStack(null);
transaction.commit();
}else{
transaction.remove(getFragmentManager().findFragmentByTag("fragmentDashboard"));
transaction.commit();
fm.popBackStack();
}
break;
case 1:
fragment = fm.findFragmentByTag("fragmentExpenses");
if (fragment == null) {
fragment = new Expenses();
transaction.replace(R.id.container, fragment, "fragmentExpenses");
transaction.addToBackStack(null);
transaction.commit();
}else{
transaction.remove(getFragmentManager().findFragmentByTag("fragmentExpenses"));
transaction.commit();
fm.popBackStack();
}
break;
default:
break;
}
}
Use this::
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
Fragment fragment = null;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
FragmentManager fm = getFragmentManager();
switch (position) {
case 0:
fragment = fm.findFragmentByTag("fragmentDashboard");
if (fragment == null){
fragment = new Dashboard();
transaction.add(R.id.container, fragment, "fragmentDashboard");
transaction.addToBackStack(null);
transaction.commit();
}else{
transaction.show(getFragmentManager().findFragmentByTag("fragmentDashboard"));
transaction.commit();
fm.popBackStack();
}
break;
case 1:
fragment = fm.findFragmentByTag("fragmentExpenses");
if (fragment == null) {
fragment = new Expenses();
transaction.add(R.id.container, fragment, "fragmentExpenses");
transaction.addToBackStack(null);
transaction.commit();
}else{
transaction.show(getFragmentManager().findFragmentByTag("fragmentExpenses"));
transaction.commit();
fm.popBackStack();
}
break;
default:
break;
}
}
Check Developers site for more info

Related

issue in initializing fragment from the activity in xamarin android?

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?

Prevent re-added a fragment once it's loaded

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.

Fragment created again, android?

I have a navigation drawer in my activity and add the fragments on drawer item clicked
For adding the fragment
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Fragment()1;
break;
case 1:
fragment = new Fragment()2;
break;
case 2:
fragment = new Fragment()3;
break;
case 3:
fragment = new Fragment()4;
break;
case 4:
fragment = new Fragment()5;
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
closeDrawer();
} else {
Logger.e("MainActivity", "Error in creating fragment");
}
Whenever I switch back from one fragment to another fragment the view is inflated again as in previous fragment was destroyed and it is being created again.
How can I stop it from being recreated?
Fragment fragment1 = null;
Fragment fragment2 = null;
Fragment fragment3 = null;
Fragment fragment4 = null;
Fragment fragment5 = null;
switch (position) {
case 0:
if (fragment1 == null) {
fragment1 = new Fragment()1;
}
break;
case 1:
if (fragment2 == null) {
fragment2 = new Fragment()2;
}
break;
case 2:
if (fragment3 == null) {
fragment3 = new Fragment()3;
}
break;
case 3:
if (fragment4 == null) {
fragment4 = new Fragment()4;
}
break;
case 4:
if (fragment5 == null) {
fragment5 = new Fragment()5;
}
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
closeDrawer();
} else {
Logger.e("MainActivity", "Error in creating fragment");
}

Fragment replacing problems

I'm having issues with fragment replacing implementing a navigation drawer.
The problem is layout overlapping, but it happens only if I let the application on the background for a long time (I asume after the activity goes to Stop).
This is the code I'm using for changing fragments. I'm not doing anything in onStop method
(Should I be doing something?)
#Override
public void onNavigationDrawerItemSelected(int position) {
if(lastPosition == position){
return;
}
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment lastFragment = fragmentManager.findFragmentByTag( lastTag );
Fragment fragment;
if ( lastFragment != null ) {
transaction.hide( lastFragment );
}
switch (position) {
case 0:
lastTag = "miPerfil";
lastPosition = 0;
fragment = fragmentManager.findFragmentByTag("miPerfil");
if(fragment != null) {
transaction.remove(fragment);
}
transaction.add(R.id.container, MiPerfilFragment.newInstance(position + 1),"miPerfil");
break;
case 1:
lastTag = "misReportes";
lastPosition = 1;
fragment = fragmentManager.findFragmentByTag("misReportes");
if(fragment != null) {
transaction.remove(fragment);
}
transaction.add(R.id.container, MisReportesFragment.newInstance(position + 1),"misReportes");
break;
case 2:
lastTag = "mapFragment";
lastPosition = 2;
fragment = fragmentManager.findFragmentByTag("mapFragment");
if(fragment != null) {
transaction.show(fragment);
}else{
transaction.add(R.id.container, MapFragment.newInstance(position + 1),"mapFragment");
}
break;
case 3:
moveTaskToBack(true);
return;
}
transaction.addToBackStack( lastTag ).commit();
}
Thanks.
there is two solution that you can try to overcome from your problem of overlapping fragment.
try (1) set a background color to your fragment in xml file.
if it does not work,
try (2) fixing this by writing your own code to remove any existing fragments before committing a new one.
for example:
fragmentManager = getFragmentManager();
ft = fragmentManager.beginTransaction();
mbFragment = new SettingsManageBooksFragment();
ft.replace(R.id.setting_detail_container2, mbFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.manage_my_books);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButtonSettingsManageBooks:
if (mPurchaseHistory == true) {
ft.remove(phFragment);
Log.d(TAG, "REMOVING PURCHASE HISTORY FRAG");
} else if (mAudio == true) {
ft.remove(aFragment);
Log.d(TAG, "REMOVING AUDIO FRAG");
} else if (mRestore == true) {
ft.remove(rFragment);
Log.d(TAG, "REMOVING RESTORE FRAG");
} else if (mCopyright == true) {
ft.remove(cFragment);
Log.d(TAG, "REMOVING COPYRIGHT FRAG");
} else if (mAbout == true) {
ft.remove(abFragment);
Log.d(TAG, "REMOVING ABOUT FRAG");
}
ft = fragmentManager.beginTransaction();
mbFragment = new SettingsManageBooksFragment();
ft.replace(R.id.setting_detail_container2, mbFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.manage_my_books);
mManageBooks = true;
mPurchaseHistory = false;
mAudio = false;
mRestore = false;
mCopyright = false;
mAbout = false;
break;
hope it will helps....
if you use first solution You may also come up with problems where the fragment behind your new fragment is still clickable. If this happens then just make the parent view of the new fragment clickable..

How to keep only one instance of a fragment, when switching with NavigationDrawer?

My App starts with a AddressFragment. From the NavigationDrawer I start (amongst others) a new AddressFragment with:
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new AddressFragment())
.addToBackStack(null)
.commit();
But I would rather just go back the first instance. How could I do that?
Or more general, how can I find out, whether an instance of a fragment already exists and then start that, if yes, otherwise create one?
When creating the fragment set a tag for it, then later you can find it through the fragment manager and replace/create accordingly.
FragmentManager fManager = getFragmentManager();
FragmentTransaction fTransaction = fManager.beginTransaction();
Fragment fragment = fManager.findFragmentByTag("uniqueTag");
// If fragment doesn't exist yet, create one
if (fragment == null) {
fTransaction.add(R.id.fragment_list, new ListFrag(), "uniqueTag");
}
else { // re-use the old fragment
fTransaction.replace(R.id.fragment_list, fragment, "uniqueTag");
}
Step one:
Optimize current code to allow a Fragment to have his own "TAG"
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new AddressFragment())
.addToBackStack(**AddressFragment.class.getName()**)
.commit();
Step two:
Somewhere in your application flow you will need to determine if a fragment exists:
public static boolean isFragmentInBackstack(final FragmentManager fragmentManager, final String fragmentTagName) {
for (int entry = 0; entry < fragmentManager.getBackStackEntryCount(); entry++) {
if (fragmentTagName.equals(fragmentManager.getBackStackEntryAt(entry).getName())) {
return true;
}
}
return false;
}
Step three:
perform fragment action
if (exists) {
// Fragment exists, go back to that fragment
//// you can also use POP_BACK_STACK_INCLUSIVE flag, depending on flow
mFragmentManager.popBackStackImmediate(AddressFragment.class.getName(), 0);
} else {
// Fragment doesn't exist
// STEP 1 + additional backstack management
}
This is tested answer, Hope this will help you
First Make these field globally in MainActivity
private static final int TIME_INTERVAL = 2000;
private long mBackPressed;
private FragmentManager fm;
private FragmentTransaction ft;
private DrawerLayout drawer;
Now in onNavigationItemSelected() implement like that
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
Class fragmentClass = null;
switch (item.getItemId()) {
case R.id.nav_home:
fragmentClass = Fragment.class;//this is MainAvctivty extends AppCompatActivity
break;
case R.id.nav_f1:
fragmentClass = FragmentOne.class;
break;
case R.id.nav_f2:
fragmentClass = FragmentTwo.class;
break;
case R.id.nav_f3:
fragmentClass = FragmentThree.class;
break;
case R.id.nav_f4:
fragmentClass = FragmentFour.class;
break;
case R.id.nav_f5:
fragmentClass = FragmentFive.class;
break;
default:
fragmentClass = Fragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {e.printStackTrace();}
//Insert the fragment by replacing any existing fragment
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
item.setChecked(true);
setTitle(item.getTitle());
drawer.closeDrawers();
return true;
}
Now handle onbackpressed like below
#Override
public void onBackPressed() {
if (drawer != null) {
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
ft.commit();
} else if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
super.onBackPressed();
} else {
Toast.makeText(this,"Press again to exit the app",Toast.LENGTH_SHORT).show();
}
mBackPressed = System.currentTimeMillis();
}
}
First time you create the fragment with a tag.
When you want to replace it try to get the fragment with tag and if it return null you create a new one

Categories

Resources