possible to load fragment.java file when using navigation drawer - java

I am just wondering if it is possible to load a fragment.java file into the MainActivity that holds all the navigation drawer code. I will include code snippets to better explain myself.
MainActivity.java
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
switch(position){
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, SuikodenFragment.newInstance(position + 1)).commit();
break;
case 1:
fragmentManager.beginTransaction()
.replace(R.id.container, SuikodenIIFragment.newInstance(position + 1)).commit();
break;
case 2:
fragmentManager.beginTransaction()
.replace(R.id.container, SuikodenIIIFragment.newInstance(position + 1)).commit();
break;
case 3:
fragmentManager.beginTransaction()
.replace(R.id.container, SuikodenIVFragment.newInstance(position + 1)).commit();
break;
case 4:
fragmentManager.beginTransaction()
.replace(R.id.container, SuikodenVFragment.newInstance(position + 1)).commit();
break;
//similar for others
}
}
The fragments in this code are also in MainActivity.java and they work. However, what I want to do is link a fragment.java file for each of these cases instead of the inline fragments listed here. The reason for this is that the fragment.java file will contain an Expandable ListView with Checkbox. Therefore, what is the best way to achieve this? Is there a line of code that will allow me load a fragment.java file?

Yes, it is possible.
You use the exact same code you are already using, but you replace SuikodenVFragment with the name of the Fragment class you want to use instead.
Say your project structure looks like this:
/com/example/myapp
MainActivity.java
SecondFragment.java
Then you can swap in an instance of SecondFragment by calling:
fragmentManager.beginTransaction().replace(R.id.container,
SecondFragment.newInstance() // or new SecondFragment(), as appropriate
).commit();
The only "catch" is that you need to make sure that you import SecondFragment at the top of MainActivity.java with the rest of your imports. However, most IDEs will take care of this for you.
If you are new to Java and haven't used an import statement or packages before, I recommend starting with the Using Package Members guide from Oracle's Java tutorials.

Related

Bottom navigation bar stop a fragment running when another one is opened

So I have no idea how to do this and even where to start, so if someone would be patient enough to explain how to do this I would be appreciated.
I already looked up on google but what I found was too difficult to understand ( and I am pretty sure that it can be done way more easily.
I have 4 fragments (4 tabs on a bottom navigation bar) and I want to have only 1 running at the time.
Now, I am pretty sure that I should use FragmentManager but I don't really know how to start to implement in my program.
Do I have to write all the FragmentTransaction code in the mainActivity? and after that how to keep goin? Is there a example code by any chance?
Disclaimer: I am a total noob in the android studio environment and I am also a student and I almost do everything as a school project.
You Can try this in your code. On the onNavigationItemSelected
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
break;
case R.id.navigation_dashboard:
fragment = new DashboardFragment();
break;
case R.id.navigation_notifications:
fragment = new NotificationsFragment();
break;
case R.id.navigation_profile:
fragment = new ProfileFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
I hope this can help You!
Thank You.

Android: how to load fragment into FrameLayout

I'm starting with Android and in my project I'm using this BottomBar. Love it, works pretty well. The code of my application is almost identical on to the one he uses in the tutorial, the only different thing is that my MainActivity extends from AppCompatActivity.
Now what I'm trying to do is to load fragments in the FrameLayout:
<!-- This could be your fragment container, or something -->
<FrameLayout
android:id="#+id/contentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomBar"
/>
To do so I'm trying this piece of code, which I found googling for the answer of my issue:
// Create new fragment and transaction
QrCodeFragment newFragment = new QrCodeFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.bottomBar, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
The line transaction.replace(R.id.bottomBar, newFragment); is complaining about the type of newFragment, which should be android.app.Fragment. My QrCode class: public class QrCodeFragment extends Fragment
As I'm starting with Android today I'm confused if I'm doing the right thing. If I should really load fragments inside the FrameLayout and if so, what am I doing wrong that I can't load it. I know it's the type of my fragment, but I don't know how to create it with the required type. I created it using Android Studio > New > Fragment > Fragment (Blank)
Thanks for any help
UPDATE:
I found the solution to my error in this post but even not having the error I still can't see the fragment.
First you have a mistake in your Fragment transaction line, according with your layout should be:
transaction.replace(R.id.contentContainer, newFragment); // not R.id.bottomBar
Second, you should use supportFragmentManager instead of fragmentManager to work with support fragments, so implement the following way:
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.contentContainer, newFragment);
transaction.addToBackStack(null);
transaction.commit();
in my case i solve it by using
androidx.fragment.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
replace your
FragmentTransaction
bottomNavigationView= findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#SuppressLint("NonConstantResourceId")
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment temp =null;
switch (item.getItemId()) {
case R.id.library:
temp = new LibraryFragment();
break;
case R.id.search:
temp = new SearchFragment();
break;
case R.id.profile:
temp = new UserprofileFragment();
break;
default:
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, temp).commit();
return true;
}
});
getSupportFragmentManager().beginTransaction().replace(R.id.fragment,new SearchFragment()).commit();

Fragment visible behind last fragment and goes on

Having Navigation drawer in Activity and changing Fragments by these lines of code
switch (menuItem.getItemId()) {
case R.id.home:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
if (getSupportFragmentManager().findFragmentByTag("ranking") == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.list_view_container, new HomeFragment(),
"ranking")
.addToBackStack("ranking")
.setTransition(
FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
} else {
getSupportFragmentManager().popBackStack("ranking", 0);
}
return true;
case R.id.current_event:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
if (getSupportFragmentManager().findFragmentByTag("ranking1") == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.list_view_container, new CurrentEvents(),
"ranking1")
.addToBackStack("ranking1")
.setTransition(
FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
} else {
getSupportFragmentManager().popBackStack("ranking1", 1);
}
return true;
}
I wanted Fragment to remain in memory so that it should not rebuilt again and again and fetch data from internet again and again so I have put the above method to get that but now, when I am changing the fragment by navigation drawer one fragment is replacing another and goes on and also I have to press back button=number of times new fragment created to exit from application. I want fragment in Backstack but not like this which is replacing one another. I want just one method at time. please help
Since you're using add(), the previous Fragment will not be removed from the container.
You should replace the calls to add() with replace(). For example, case R.id.home would become:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.list_view_container, new HomeFragment(),
"ranking")
.addToBackStack("ranking")
.setTransition(
FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
I wanted Fragment to remain in memory so that it should not rebuilt again and again and fetch data from internet again and again
You could try Fragment's setRetainInstance(true) to do that but it's not recommended (and I've never used it).
The other way which is more appropriate is to use Fragment's onSaveInstanceState(). You can read about it at Developer Android.
You can Check This Scenario, Hopefully it solve your Problem:-
Try to Attach and Detach the Fragment from FragmentTransaction. When
you Move to Next Fragment detach the last one Fragment From
FragmentTransaction and when you wants to use it again then attach it.
See sample code for how to Detach and Attach the Fragment, It will
help you :
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
AndroidFragment androidFragment = (AndroidFragment) fm.findFragmentByTag("android");
AppleFragment appleFragment = (AppleFragment) fm.findFragmentByTag("apple");
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
/** Detaches the androidfragment if exists */
if(androidFragment!=null)
ft.detach(androidFragment);
/** Detaches the applefragment if exists */
if(appleFragment!=null)
ft.detach(appleFragment);
/** If current tab is android */
if(tabId.equalsIgnoreCase("android")){
if(androidFragment==null){
/** Create AndroidFragment and adding to fragmenttransaction */
ft.add(R.id.realtabcontent,new AndroidFragment(), "android");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(androidFragment);
}
}else{ /** If current tab is apple */
if(appleFragment==null){
/** Create AppleFragment and adding to fragmenttransaction */
ft.add(R.id.realtabcontent,new AppleFragment(), "apple");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(appleFragment);
}
}
ft.commit();
`For Reference see this onTabChanged :` [http://wptrafficanalyzer.in/blog/creating-navigation-tabs-using-tabhost-and-fragments-in-android/][1]

Check if a fragment exists and reuse it

I'm using the following code to create a fragment everytime the user click on an item in a list view.
But in this way the fragment is created at every user click. What I want is to reuse the old fragment (if it exists) and only reload its content (don't create a new one).
MagazineViewFragment fragment = new MagazineViewFragment();
fragment.openStream(itemSelected);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
How can I do?
There're multiple ways, probably the most easy one is to check if the current Fragment in your container is an instance of FragmentXYZ (in your case MagazineViewFragment).
Example
Fragment mFragment = getFragmentManager().findFragmentById(R.id.container);
if (mFragment instanceof MagazineViewFragment)
return;
Add tag when you call your fragment from activity:
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentByTag( MagazineViewFragment.TAG);
if (fragment == null) {
MagazineViewFragment fragment = new MagazineViewFragment();
fragment.openStream(itemSelected);
getFragmentManager()
.beginTransaction()
.add(R.id.container, fragment, MagazineViewFragment.TAG)
.commit();
}
If you need only to update itemSelected - see broadcasts or listeners.
Something like this might help:
getFragmentManager().findFragmentById(fragmentId);
Do not forget the null check.

FragmentTransaction can't replace fragment working with ABS

I have an activity called HomeActivity,
{
public class HomeActivity extends SherlockFragmentActivity
}
but the code below here
{
private void selectItem(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Locate Position
switch (position) {
case 0:
ft.replace(R.id.content_frame, home);
break;
case 1:
ft.replace(R.id.content_frame, gadget);
break;
case 2:
ft.replace(R.id.content_frame, news);
break;
case 3:
ft.replace(R.id.content_frame, techno);
break;
case 4:
ft.replace(R.id.content_frame, game);
break;
case 5:
ft.replace(R.id.content_frame, tips);
break;
case 6:
ft.replace(R.id.content_frame, cgt);
break;
case 7:
ft.replace(R.id.content_frame, info);
break;
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
mDrawerList.setItemChecked(position, true);
// Close drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
the FragmentTransaction can't replace my fragment in SherlockFragmentActivity.
I have no idea with this, I search and implements some method from google and stackoverflow but my issue still not sot
please I need a hint with this..thanks
When working with compatibility frameworks like ActionBarSherlock and Google Support Library
it's easy to mix up imports with the native APIs.
Just take the FragmentTransaction class for example.
Make sure you are not importing the native version.
import android.app.FragmentTransaction;
Instead make sure you import the version from the support library.
import android.support.v4.app.FragmentTransaction;
Actually check all your imports that you use in your project making
sure you are picking the right ones (support.v4.app).

Categories