i am trying to add some extra fragments to the existing code its not showing the other fragment. but toast is showing the clicked element id
koteswaradk opened this issue · comments
here is the code please let me know what is the the issue. i am using android studio 3.1 and its supporting library
public class MainActivity extends AppCompatActivity implements OnMenuItemClickListener
, OnMenuItemLongClickListener , FragmentLauncherFusion.OnFragmentInteractionListener
,FragmentMappedData.OnFragmentInteractionListener,FragmentFusionTemplateDetails.OnFragmentInteractionListener,FragmentChangeLog.OnFragmentInteractionListener,FragmentFeedBack.OnFragmentInteractionListener,
FragmentReadme.OnFragmentInteractionListener
{
private FragmentManager fragmentManager;
private ContextMenuDialogFragment mMenuDialogFragment;
String selectedString;
TextView mToolBarTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
initToolbar();
initMenuFragment();
addFragment(new MainFragment(), true, R.id.container);
// getSupportFragmentManager().beginTransaction().replace(R.id.container, new FragmentLauncherFusion()).commit();
}
private void initMenuFragment() {
MenuParams menuParams = new MenuParams();
menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
menuParams.setMenuObjects(getMenuObjects());
menuParams.setClosableOutside(false);
mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
mMenuDialogFragment.setItemClickListener(this);
mMenuDialogFragment.setItemLongClickListener(this);
}
private List<MenuObject> getMenuObjects() {
// You can use any [resource, bitmap, drawable, color] as image:
// item.setResource(...)
// item.setBitmap(...)
// item.setDrawable(...)
// item.setColor(...)
// You can set image ScaleType:
// item.setScaleType(ScaleType.FIT_XY)
// You can use any [resource, drawable, color] as background:
// item.setBgResource(...)
// item.setBgDrawable(...)
// item.setBgColor(...)
// You can use any [color] as text color:
// item.setTextColor(...)
// You can set any [color] as divider color:
// item.setDividerColor(...)
List<MenuObject> menuObjects = new ArrayList<>();
MenuObject close = new MenuObject();
close.setResource(R.drawable.icn_close);
MenuObject send = new MenuObject("Launcher (Fusion)");
send.setResource(R.drawable.icn_1);
MenuObject like = new MenuObject("Mapped data");
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.icn_2);
like.setBitmap(b);
MenuObject addFr = new MenuObject("Fusion Template details");
BitmapDrawable bd = new BitmapDrawable(getResources(),
BitmapFactory.decodeResource(getResources(), R.drawable.icn_3));
addFr.setDrawable(bd);
MenuObject addFav = new MenuObject("Change Log");
addFav.setResource(R.drawable.icn_4);
MenuObject block = new MenuObject("Feed back");
block.setResource(R.drawable.icn_5);
MenuObject readme = new MenuObject("Readme");
readme.setResource(R.drawable.icn_5);
menuObjects.add(close);
menuObjects.add(send);
menuObjects.add(like);
menuObjects.add(addFr);
menuObjects.add(addFav);
menuObjects.add(block);
menuObjects.add(readme);
return menuObjects;
}
private void initToolbar() {
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolBarTextView = (TextView) findViewById(R.id.text_view_toolbar_title);
setSupportActionBar(mToolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
mToolbar.setNavigationIcon(R.drawable.btn_back);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
protected void addFragment(Fragment fragment, boolean addToBackStack, int containerId) {
invalidateOptionsMenu();
String backStackName = fragment.getClass().getName();
boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStackName, 0);
if (!fragmentPopped) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(containerId, fragment, backStackName)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
if (addToBackStack)
transaction.addToBackStack(backStackName);
transaction.commit();
}
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem settingsItem = menu.findItem(R.id.context_menu);
// set your desired icon here based on a flag if you like
settingsItem.setIcon(getResources().getDrawable(R.drawable.btn_add));
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.context_menu:
if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
}
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (mMenuDialogFragment != null && mMenuDialogFragment.isAdded()) {
mMenuDialogFragment.dismiss();
} else {
finish();
}
}
@Override
public void onMenuItemClick(View clickedView, int position) {
// Toast.makeText(this, "Clicked on position: " + position, Toast.LENGTH_SHORT).show();
// Fragment fragment = null;
switch (position){
case 0:
break;
case 1:
Toast.makeText(this, "Clicked on position: " + 11, Toast.LENGTH_SHORT).show();
mToolBarTextView.setText("Launcher: Fusion");
// fragment= new FragmentLauncherFusion();
addFragment(new FragmentLauncherFusion(), true, R.id.container);
break;
case 2:
Toast.makeText(this, "Clicked on position: " + 22, Toast.LENGTH_SHORT).show();
mToolBarTextView.setText("Launcher: Mapped data");
// fragment= new FragmentMappedData();
addFragment(new FragmentMappedData(), true, R.id.container);
break;
case 3:
Toast.makeText(this, "Clicked on position: " + 33, Toast.LENGTH_SHORT).show();
mToolBarTextView.setText("Launcher: Fusion template details");
// fragment= new FragmentFusionTemplateDetails();
break;
case 4:
Toast.makeText(this, "Clicked on position: " + 44, Toast.LENGTH_SHORT).show();
mToolBarTextView.setText("Launcher: Change Log");
// fragment= new FragmentChangeLog();
break;
case 5:
Toast.makeText(this, "Clicked on position: " + 55, Toast.LENGTH_SHORT).show();
mToolBarTextView.setText("Launcher: Feed Back");
// fragment= new FragmentFeedBack();
break;
case 6:
Toast.makeText(this, "Clicked on position: " + 66, Toast.LENGTH_SHORT).show();
mToolBarTextView.setText("Launcher: Readme");
// fragment= new FragmentReadme();
break;
}
/* if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, fragment);
ft.commit();
}*/
}
@Override
public void onMenuItemLongClick(View clickedView, int position) {
// Toast.makeText(this, "Long clicked on position: " + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onFragmentInteraction(Uri uri) {
}