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).
Related
When I pause the app for a few moments the activity's Fragmnetos are recreated
public class CopycatActivity extends FragmentActivity implements CopycatInterface {
public void flipFragment(int type) {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
switch (type) {
case 1:
ft.replace(R.id.container, zWaitFragment);
break;
case 2:
ft.replace(R.id.container, zMasterMode);
break;
case 3:
ft.replace(R.id.container, zCopyMode);
break;
case 4:
ft.replace(R.id.container, zRateDraws);
break;
case 5:
ft.replace(R.id.container, zWinnerFragment);
break;
case 6:
ft.replace(R.id.container, zSleepMode);
break;
}
ft.commitAllowingStateLoss();
}
}
Depending on which fragment I am in, when paused it is reset
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.
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();
So here's the code:
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new DoctorFragment();
break;
case 2:
fragment = new HospitalFragment();
break;
case 3:
fragment = new SpecialClinicFragment();
break;
case 4:
fragment = new PharmacyFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
//the parameter "fragment" in this line of code gives an error message.
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
getActionBar().setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
It seems like I haven't properly initialised fragment instance.
The error message was "Wrong 2nd argument type" but I'm pretty sure I put in the right parameter.
Problem is not with the argument.You have used Fragment from
android.support.v4 library but you are making transaction on android.app.fragment.
Solution:
import android.app.fragment instead of android.support.v4.fragment
Because you cannot mix up with support.v4 library and android.app.*
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.