I am using this sliding menu in my project
https://github.com/jfeinstein10/SlidingMenu
I am adding this menu to all of my activities. And this sliding menu is calling a Fragment at opening. How to prevent creating multiple fragments for each activity and call if Fragment is already created at back stack ?
Each of my activities are extending my BaseActivity and i'm initialzing sliding menu like this at BaseActivity.
getSupportFragmentManager()
.beginTransaction().replace(R.id.detail, new DetailFragment())
.commit();
Thanks
I would (at least) try to implement only one activity and replace various fragments within the content frame. You should know that the example contains various approaches from one you should choose one ;)
So having a menu and content fragment you could start as below (compare ResponsiveUIActivity too).
public class StartActivity extends SlidingFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Fragment contentFragment = InitialFragment()
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, contentFragment).commit();
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame, new MenuFragment()).commit();
...
}
public void switchContent(final Fragment fragment) {
// replace fragment in content frame
...
}
In the menu fragment, you can create new fragments according to the users choice (compare BirdMenuFragment):
public class MenuFragment extends ListFragment {
@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
Fragment newContent = null;
switch (position) {
case 1 : new ThisFragment();
break;
case 2 : new ThatFragment();
break;
case 3 : new AnotherFragment();
break;
}
if (newContent != null) switchFragment(newContent);
}
private void switchFragment(Fragment fragment) {
// call StartActivity.switchContent
...
}
Finally, the glue between activity and menu fragment is MenuFragment.switchFragment(Fragment fragment) and StartActivity.switchContent(final Fragment fragment) whereas the menu fragment method calls the activity method for replacing the content fragment in the content frame (compare classesBirdMenuFragment and ResponsiveUIActivity in the example).
This is just an outline of how you could implement the SlidingMenu and how you could replace the fragments in the content frame. There are of course some challenges left to cope with.
Anyway I hope I could help a bit ... Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With