is this possible ? I use framelayout to display the contents of my fragment with the use of navigation drawer. What i want to happen is to keep the state of the fragment even if i go to another fragment.
I have a vague idea on whats causing the reload which is this.
private Fragment getProfileFragment() {
switch (navItemIndex) {
case 0:
// home
ProfileLO profileLO = new ProfileLO();
return profileLO;
case 1:
LocationLO locationLO = new LocationLO();
return locationLO;
case 2:
FarmGoodsLO farmGoodsLO = new FarmGoodsLO();
return farmGoodsLO;
case 3:
RentEquipLO rentEquipLO = new RentEquipLO();
return rentEquipLO;
default:
return new ProfileLO();
}
}
Is there any hack out there?
Related
This question already has answers here:
Fragment with multiple backstack
(4 answers)
Closed last year.
Hello i want to manage BackStack in fragment i have implemented a method but im not happy with it becuase if i switch to multiple fragments again and again ,then i want that backstack dont switch to the same fragment again and again and just goes to each fragments one time backward and in the last to the Home_Fragment and then exit
For Eg : if i switch from notification to profile and make it like a loop by pressing it again and again ,and when i start pressing back it also follows the same loop that i dont want ,i want that even user do a thing like this the back stack only goes to each fragment only once and at the last goes to the Home Fragment and exit
Just like a big app like instagram or pintrest
here is a code
private final BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
item -> {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new Home_Fragment();
break;
case R.id.nav_following:
selectedFragment = new Following_Fragment();
break;
case R.id.nav_upload:
selectedFragment = new Upload_Fragment();
break;
case R.id.nav_notification:
selectedFragment = new Notification_Fragment();
break;
case R.id.nav_profile:
selectedFragment = new Profile_Fragment();
break;
}
assert selectedFragment != null;
getSupportFragmentManager().beginTransaction().addToBackStack(String.valueOf(selectedFragment)).replace(R.id.fragment_container,
selectedFragment).commit();
return true;
};
I'm having difficulty understanding the question/problem, but if you want to avoid re-adding a fragment that is already added then you could check the stack before adding a new fragment is it is already there. This article might be helpful.
use popBackStackImmediate("your-tag",0)
or findFragmentByTag in fragmentManager and replace it(be careful about adding to back stack again and again).
This is not really a problem but I just want to know am I using a good approach?
I am developing an application, and in that app I am using fragments. Only one activity and multiple fragments. The thing is, my activity is not a FragmentActivity because I've some views "Controls" on that activity. In the mid of the activity layout I am replacing fragments. Mock up UI is shown below.
So the approach I am using is that I have a public method in my main activity loadFragment(String fragment), I call this method from any fragment to load a particular fragment. I get instance of my activity and call this method like this.
MainActivity.getInstance().laodFragment(framgnet);
Instance is basically this.
private MainActivity instance;
And
public static MainActivity getInstance(){
return instance;
}
Here my loadFragment() method.
public void loadFragment(String fragment) {
CURRENT_FRAGMENT = fragment;
switch (fragment) {
case FRAGMENT_WORK:
generalFragment = new WorkFragment();
break;
case FRAGMENT_WORK_TWO:
generalFragment = new WorkTwoFragment();
break;
case FRAGMENT_REGISTRATION:
generalFragment = new RegistrationFragment();
break;
case FRAGMENT_INTERNAL:
generalFragment = new InternalFragment();
break;
case FRAGMENT_ABSENCE:
generalFragment = new AbsenceFragment();
break;
case FRAGMENT_OVERTIME:
generalFragment = new OvertimeFragment();
break;
case FRAGMENT_HOME:
clearAllFragments();
generalFragment = new HomeFragment();
}
replaceFragment(generalFragment);
}
I just want to know am I using a good approach? if not how can I improve it?
I've already read that saving contexts in static variables is not a good approach.
Thank you.
I've already read that saving contexts in static variables is not a
good approach
Correct ! static mostly good for cache.
You can perform another alternative to make it tightly coupled like below.
1: Make an Interface to bridge-up Activity and Fragment.
interface BridgeInteface
{
public void loadFragment(String fragment);
}
2: Implement that Interface to your MainActivityand Override method loadFragment(String fragment)
3: Pass the instance of Interface as parameter into the Fragment
#Override
public void loadFragment(String fragment) {
CURRENT_FRAGMENT = fragment;
switch (fragment) {
case FRAGMENT_WORK:
generalFragment = new WorkFragment(bridgeInteface);
break;
case FRAGMENT_WORK_TWO:
generalFragment = new WorkTwoFragment(bridgeInteface);
break;
case FRAGMENT_REGISTRATION:
generalFragment = new RegistrationFragment(bridgeInteface);
break;
case FRAGMENT_INTERNAL:
generalFragment = new InternalFragment(bridgeInteface);
break;
case FRAGMENT_ABSENCE:
generalFragment = new AbsenceFragment(bridgeInteface);
break;
case FRAGMENT_OVERTIME:
generalFragment = new OvertimeFragment(bridgeInteface);
break;
case FRAGMENT_HOME:
clearAllFragments();
generalFragment = new HomeFragment(bridgeInteface);
}
replaceFragment(generalFragment);
}
4: Call method loadFragment(String fragment) from your Fragment when you need to replace Fragment
Okay I found the solution to my problem. I removed all static instances from my Fragments and Activity and replaced all method calls with Event Calls. I used Otto library for this purpose. And it is extremely useful and easy to use.
I'm developing an android application, I have a problem with the changing of back button function. How i can "replace" the default action of this button ?
I would realize a simple function that allow the user to return to the previous fragment when back button is pressed, for example :
[Main Fragment] ----> [2nd Fragment]
[2nd Fragment] <---- [Main Fragment]
I have read some question like this, but i don't understand how to solve my problem.
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment(getApplicationContext());
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
}
This is the part of my code where i call the "new fragment".
When i start up my application a new fragment is created (HomeFragment), in this frag i create a new fragment (Fragment_Detail).
What that i want is return from Fragment_Detail to HomeFragment.
I hope I was explanatory
Just add the addToBackStack(null). It will automatically go to the previous fragment loaded in stack.
HomeFragment fragment = new HomeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.replace(R.id.content, fragment,TAG)
.commit();
Refer addToBackStack() and refer this answer.
Refer the docs
You should take a look at the example: http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments
I have a drawer navigation with:
private void DisplayView (int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
...
From an activity I can throw that fragment (fragment2 for example)? With BeginTransaction?
Thanks
A Fragment cannot exist in it's own, eg. without an Activity. It cannot be fired up with an Intent like Activities do. You have to either create a new Activity to hold your new Fragment or replace the Fragment of your current Activity with the new one.
You cannot start fragment as an activity, they need to be added to activities.
Read more in the docs here
Also, see this
Ok so,I'm new to Android programing and I have a question. I have a swipeview similar to the one in the Play Store,and I want each tab to contain a different listview fragment. I have 2 fragments ,one called LP and other PL ,lets say. How to I edit this part of code so it uses my fragments ?
#Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new Fragment1();
case 1:
return fragment = new Fragment2();
case 2:
return fragment = new Fragment3();
default:
break;
}
return fragment;
EDIT : So after listening to the advice given,it still doesent work. :-?? I have 2 separate java files for my fragments and it doesent seem to work unless i copy the classes and put them inside the main activity...I may be missing something ,so please feel free to point out any posibility
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment fragment = new Fragment1();
return fragment;
case 1:
Fragment fragment = new Fragment2();
return fragment;
case 2:
ListFragment fragment = new Fragment3();//for list fragments
return fragment;
default:
break;
}
return fragment;
You could use something like this:
#Override
public Fragment getItem(int position) {
Fragment result;
switch (position) {
case 0:
result = new LPFragment();
case 1:
result = new PLFragment();
default:
result = new DefaultFragment();
}
return result
}
You just need to return an instance of whatever fragment you want for each case in the switch.
If the PL and LP classes extends from Fragment then it's fine (though you may have some need to cast them elsewhere in the code as the function returns Fragment and not your own class types)