I am using jfeinstein10 library and sample project. I want to show some menu option in left sliding menu and also some other option menu in the right sliding menu. But i am not able to do that . Here is the code :
package com.slidingmenu.example;
public class LeftAndRightActivity extends BaseActivity {
public LeftAndRightActivity() {
super(R.string.left_and_right);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
setContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new SampleFragment())
.commit();
getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame_two, new SampleFragment())
.commit();
}
}
Here it is what i have set in the onCreate :
getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
setContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new SampleListFragment())
.commit();
getSlidingMenu().setMenu(R.layout.menu_frame_two);
getSlidingMenu().setShadowDrawable(R.drawable.shadowright);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame_two, new SampleListFragment2())
.commit();
getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame_two, new SampleListFragment2())
.commit();
here whenever i use this my program get crashed.And here is my logcat :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.slidingmenu.example/com.slidingmenu.example.LeftAndRightActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c for fragment Fragment{41260b30 #0 id=0x7f05003c}
Related
if (User.getUser(getContext()).getToken()!=null){
Intent intent =new Intent(getContext(),UserProfileActivity.class);
startActivity(intent);}
Then I open a fragment from that activity
mCvAboutUS.setOnClickListener(v -> {
Bundle bundle = new Bundle();
bundle.putString("aboutUs", "aboutUs");
mFragment = new TextDisplayFragment();
mFragment.setArguments(bundle);
llMainProfile.setVisibility(View.GONE);
replaceFragment(mFragment);
here the method replaceFragment
public void replaceFragment(Fragment fragment) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.frameLayout, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
}
Just activities handle back-pressed event.
So you should handle back-pressed event in your fragment.
Take look at following link:
How to implement onBackPressed() in Fragments?
I've been trying to make the Transition Animations work with Fragments, what I find weird (or maybe I'm not understanding something right) is that when using replace it works as expected, but when using add it doesn't...
WORKS:
EndFragment fragment = new EndFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.addToBackStack(null)
.addSharedElement(imageView, imageTransitionName)
.addSharedElement(textView, textTransitionName)
.replace(R.id.container, fragment)
.commit();
DOESN'T WORK:
EndFragment fragment = new EndFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.addToBackStack(null)
.addSharedElement(imageView, imageTransitionName)
.addSharedElement(textView, textTransitionName)
.add(R.id.container, fragment)
.commit();
Any ideas?
I'm trying avoid adding the same fragment to backStack with this method:
public static void replaceFragment(FragmentManager fragmentManager, Fragment fragment, Boolean addToBackStack) {
String backStateName = fragment.getClass().getName();
boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
if (addToBackStack && !fragmentPopped && fragmentManager.findFragmentByTag(backStateName) == null) {
fragmentManager
.beginTransaction()
// .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.fade_in, android.R.anim.fade_out)
.replace(R.id.container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(backStateName) // was 'backStateName'
.commit();
} else {
if (!addToBackStack)
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager
.beginTransaction()
// .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.fade_in, android.R.anim.fade_out)
.replace(R.id.container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.disallowAddToBackStack()
.commit();
}
}
Navigation Drawer contains: ImageView with profilePhoto which opens ProfileMainFragment and few categories which opens MainFragmentCategory. Boolean addToBackStack is false when fragment is choosen from navigationDrawerMenu and true when is choosen within fragment (move from MainFragmentCategory to DetialFragmentCategory) or click profilePhoto in navigationDrawer.
fragmentPopped is always false, why is that so? Even if I click profilePhoto and again profilePhoto in navigationDrawer. It should avoid to add it to backStack for a second (and third, and fourth...) time, but it didn't.
Any idea how can I make it right?
You can add the following code before you replace your fragment.
// Replace fragmentCotainer with your container id
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragmentCotainer);
// Return if the class are the same
if(currentFragment.getClass().equals(fragment.getClass())) return;
I'm adding a Fragment to my Activity like this:
getSupportFragmentManager()
.beginTransaction()
.add(R.id.frame_container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(fragment.getClass().getName())
.commit();
But when I want to find the Fragment using a FragmentManager it's returning null:
Fragment oldFragment = (Fragment) getSupportFragmentManager().findFragmentByTag(fragment.getClass().getName());
You try to find it by tag, but you haven't given it any tag
if you want to give it a tag, do it like this
getSupportFragmentManager()
.beginTransaction()
.add(R.id.frame_container, fragment, "tagABC")
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(fragment.getClass().getName())
.commit();
and then you can get it with
Fragment oldFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("tagABC");
BTW, you should correct your question title, the problem has nothing related to backstack.
I have a nav fragment and a content fragment. I want to be able to pass a bundle, and reload the fragment based on fragment interactions. I've seen people say use .setArguments(), but it's not letting me use said method. Any advice?
public void onStockFragmentInteraction(String stock) {
Bundle bundle = new Bundle();
bundle.putString("stock", stock);
getFragmentManager()
.beginTransaction()
.replace(R.id.content, new StoryFragment())
.addToBackStack(null)
.commit();
}
setArguments is a method of Fragment not of FragmentManager
StoryFragment f = new StoryFragment()
f.setArguments(bundle);
getFragmentManager()
.beginTransaction()
.replace(R.id.content, f)
.addToBackStack(null)
.commit();
keep a reference to the fragment and call setArguments on the instance before submitting it on the transaction