how to reloading fragment after transaction - android

I am using swipe view with ViewPager to display tabs in project.
I am adding three tabs(fragments) to AlertTabsPagerAdapter using
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new FragmentA();
case 1:
// Games fragment activity
return new FragmentB();
case 2:
// Movies fragment activity
return new FragmentC();
}
return null;
}
I am having two problems:
By default OffscreenPageLimit of ViewPager is two, so in my case for first time FragmentA and FragmentB will get created. I want to reload or recreate FragmentB on swipe from FragmentA. How can I achieve this?
In the first tab (i.e. FragmentA) I am having multiple fragments like FragmentA through FragmentD. For this I am replacing FragmentA with FragmentD using FragmentTransaction.
After the fragment transaction onback key press from FragmentD i am getting back to FragmentA, but now I want to reload or recreate FragmentA.

Related

Return from activity to last visited intent

I have a main activity that exists out of three fragments. Fragment 2 is the main fragment. On fragment 3 I have a button. Once I click the button it directs the user to a ChatActivity. The ChatActivity has an onBackButtonPressed that should return the user back to fragment 3. However, it seems that it would always return the user to fragment 2 (the main fragment).
How can I bring the user to the fragment they last visited, or at least back to fragment 3?
Edit:
I added this block of code in the button onClick function:
ChatFragment fragment = new ChatFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.main_tabPager, fragment);
transaction.addToBackStack(null);
transaction.commit();
When I click the back button in the activity it does not return me to fragment 3 but instead rebuild the fragmentpager and start back at Fragment 2.
When you are opening fragment 3 from main fragment (fragment 2), add fragment 3 into backstack like this:
Fragment3 fragment3 = new Fragment3();
getSupportFragmentManager().beginTransaction().add(R.id.content, fragment3).addToBackStack(null).commit();
You should add all fragments to backstack that you want to return to
Ideally addToBackStack() on fragment transaction should be enough as per documentation, but it seems not true at times, so we have to handle the popping up of the back stack upon Back button pressed by ourselves. I added this to my activity and it worked as expected:
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0 ){
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
Hope it helps.

Fragment and BackStack not working properly

My MainActivity contains a Navigation Drawer with two fragments. The first fragment is loaded automatically when the app starts. I want the app to switch from fragment two to fragment one when the back button is pressed or if the first fragment is added then exit the app. I am adding the fragmentTransaction of the first fragment to a stack and then calling popBackStack in my onBackPressed method. However the behaviour is pretty weird.
When I'm on the first fragment the application should exit (ie, execute super.onBackPressed) however when on the first fragment and pressing back the first fragment is removed from the fragmentholder leading to a blank screen and then on pressing the second time the app closes.
When I'm on the second fragment nothing happens when the back button is pressed the first time and on pressing the back button a second time the app closes. Here's the relevant code from the MainActivity.java
#Override
public void onBackPressed() {
if (musicService.isPng()) moveTaskToBack(true);
if (getFragmentManager().getBackStackEntryCount() > 0)
getFragmentManager().popBackStack("returnFragment", 0);
else super.onBackPressed();
}
private void loadSelection(int i) {
navList.setItemChecked(i, true);
switch (i) {
case 0:
FirstFragment firstFragment = new FirstFragment();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentholder, firstFragment)
.addToBackStack("returnFragment")
.commit();
break;
case 1:
SecondFragment secondFragment = new SecondFragment();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentholder, secondFragment)
.commit();
break;
case 2:
musicService.removeNotification();
musicService.stopSelf();
MainActivity.this.finish();
}
}
loadSelection(0) is called in the onCreate of the MainActivity
Unlike given in the code, I have tried various modes of implementing the popBackStack() method but all of them lead to the same result. Just to add I don't want to implement a workaround since there are only 2 fragments since I am already working on adding new fragments.
try this,
call method .addToBackStack("returnFragment") while loading second fragment and remove it from first transaction.

how to effectively implement ViewPager with different fragments?

So far I have only implemented ViewPager with one type of fragment.
Now I want to add navigation tabs and be able to slide sideways from fragment of type A to fragment of type B . Do I need to contain both types of fragments in one activity ? If so , does it matter which fragment will have the view pager?
thank you.
Neither of the fragments contain the ViewPager. That would be contained in this case in your activity. The navigation tabs sit above the Viewager. Look at this tutorial: https://github.com/codepath/android_guides/wiki/Sliding-Tabs-with-PagerSlidingTabStrip . You can specify what fragments go in the ViewPager in the getItem method of a FragmentPagerAdater.
#Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position) {
case 0:
fragment = fragment1.newInstance();
break;
case 1:
fragment = fragment2.newInstance();
break;
case 2:
fragment = fragment3.newInstance();
break; }
return fragment;
}

How to Save / Restore nested fragments in android?

The layout file of my main activity contains a in wich I'm loading
fragments dinamically using NavigationDrawer.
To achive this I use a FragmentPagerAdapter:
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
}
return null;
}
In FragmentA there is a ViewPager + another FragmentPagerAdapter, so you can swipe
between three other fragments. (Frament1, Fragment2, Fragment3).
It works like this:
(I can't insert the image due to the lack of reputation...)
http://62.165.232.86:1991/images/fragments.png
When I swap between Frament1, Fragment2 and Fragment3, it works fine.
(I set the ViewPager's offScreenPageLimit high enough.)
The problem occures when I navigate to FragmentB or FragmentC and then FragmentA gets destroyed and I dont know how to save its instance and recover it, becaues I dont have
reference to Fragment1.
EDIT 1:
I swap between FragmentA and FragmentB like this:
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main, mAdapter.getItem(0));
tx.commit();
Im a bit confused. I should save the instances of Fragment1, 2, 3 and Fragment A,
and recover them all when I navigate back to Fragment A but I don't know how because I dont have reference to the child fragments.
Can you help me?
Thanks for your answers,
Daneel Olivaw
Idea (I not sure at 100%):
Fragment fragment = getFragmentManager().findFragmentByTag("yourtag");
if (fragment==null) {
fragment = mAdapter.getItem(0)
}
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.add(R.id.main, fragment, "yourtag");
tx.commit();

Fragment transaction on a single tab of ViewPager in SherlockFragmentActivity

I am creating ViewPager in SherlockFragmentActivity.
Structure of my app is like ViewPager->having three tabs: tab1, tab2, tab3.
In tab2, I want to display different fragments like fragment1->fragment2->fragment3.
In ViewPager, I am having three tabs (Fragments) added using FragmentStatePageAdapter as shown below.
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
SearchTab searchtab = new Tab1();
return tab1;
case 1:
BrowseTab browsetab = new Tab2();
return tab2;
case 2:
SavedItemsTab savedTab = new Tab3();
return Tab3;
}
return null;
}
Now in tab2, I have displayed ListView in fragment1. When user clicks an item from ListView, I want to display another fragment (fragment2) with other ListView. I am doing this by fragment transaction as bellow:
Fragment newFragment = new Brxxxx_Fragment();
Bundle args = new Bundle();
args.putString("Main_Cat_ID", Main_Category_ID_Str);
args.putString("Main_Cat_Name", Main_Category_Name_Str);
newFragment.setArguments(args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.b_p_layoutid, newFragment);
transaction.addToBackStack(null);
transaction.commit();
After this step, I am able to display another fragment (fragment2), but my old fragment (fragment1) is still visible below the new fragment.
If I want to remove previous fragment without using replace(), how should I get the current fragment to pass in remove method?
Maybe you can remove the other fragment if you don't need to navigate back or set background color to the new fragment . You can check that your layout id 'R.id.b_p_layoutid' is of your 'FrameLayout' and not your 'Fragment' id just in case...
android:background="?android:attr/colorBackground"
getActivity().getSupportFragmentManager().beginTransaction().remove('YOUR_FRAGMENT').commit();
You can check this answer too:
Visit Android replace fragment still displays some of the replaced fragment
Visit Fragment replaced still visible on background

Categories

Resources