1. Are you ready for the Galaxy S20? Here is everything we know so far!

Not working context menu item in fragment

Discussion in 'Android Development' started by masterpda13, Aug 14, 2017.

  1. masterpda13

    masterpda13 Lurker
    Thread Starter

    Code (Java):
    1. public class ThreeFragment extends Fragment {
    2. public View ThreeFragmentView = null;
    3. public static TextView internalMemoryInfo;
    4. public static Button IntBtn;
    5.  
    6. public ThreeFragment() {
    7.     // Required empty public constructor
    8. }
    9.  
    10. @Override
    11. public void onCreate(Bundle savedInstanceState) {
    12.     super.onCreate(savedInstanceState);
    13. }
    14.  
    15. @Nullable
    16. @Override
    17. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    18.     if (ThreeFragmentView == null) ThreeFragmentView = inflater.inflate(R.layout.fragment_three, container, false);
    19.     internalMemoryInfo = (TextView) ThreeFragmentView.findViewById(R.id.textViewInternalMemoryInfo);
    20.     IntBtn = (Button) ThreeFragmentView.findViewById(R.id.InternalBtn);
    21.     return ThreeFragmentView;
    22. }
    23.  
    24. public static void showText(String text) {
    25.     internalMemoryInfo.setText(text);
    26. }
    27.  
    28. @Override
    29. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    30.     super.onViewCreated(view, savedInstanceState);
    31.     registerForContextMenu(IntBtn);
    32. }
    33.  
    34. @Override
    35. public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    36.     switch (v.getId()){
    37.         case R.id.InternalBtn:
    38.             menu.setHeaderTitle("Internal Memory Menu");
    39.             menu.add(0, ClearCacheMet, 0, "Clear Cache");
    40.             menu.add(0, Common, 0, "Go to Common Tab");
    41.             menu.add(0, ITEM_CANCEL, 0, "Cancel");
    42.             break;
    43.     }
    44. }
    45.  
    46. @Override
    47. public boolean onContextItemSelected(MenuItem item) {
    48.     switch (item.getItemId()) {
    49.         case ClearCacheMet:
    50.             ClearCache();                   <-------This not working
    51.             break;
    52.         case Common:
    53.             tabLayout.getTabAt(0).select(); <-------This working
    54.             break;
    55.         case ITEM_CANCEL:
    56.             ins.closeContextMenu();         <-------This working
    57.             break;
    58.     }
    59.     return true;
    60. }
    61.  
    62. public static boolean deleteDir(File dir){
    63.     if (dir != null && dir.isDirectory()) {
    64.         String[] children = dir.list();
    65.         for (int i = 0; i < children.length; i++) {
    66.             boolean success = deleteDir(new File(dir, children[i]));
    67.             if (!success) {
    68.                 return false;
    69.             }
    70.         }
    71.     }
    72.     return dir.delete();
    73. }
    74.  
    75. public static void ClearCache() {
    76.     File cache = ins.getCacheDir();
    77.     File appDir = new File(cache.getParent());
    78.     if (appDir.exists()) {
    79.         String[] children = appDir.list();
    80.         for (String s : children) {
    81.             if (!s.equals("lib")) {
    82.                 deleteDir(new File(appDir, s));
    83.             }
    84.         }
    85.     }
    86.  
    87.     try{
    88.         /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    89.             ((ActivityManager) ins.getSystemService(Context.ACTIVITY_SERVICE)).clearApplicationUserData();
    90.         } else {*/
    91.             File externalcache = ins.getExternalCacheDir();
    92.             File externalappDir = new File(externalcache.getParent());
    93.             if (externalappDir.exists()) {
    94.                 String[] children = externalappDir.list();
    95.                 for (String s : children) {
    96.                     if (!s.equals("lib")) {
    97.                         deleteDir(new File(externalappDir, s));
    98.                     }
    99.                 }
    100.             } else {
    101.                 Log.d(TAG, "No externalappDir");
    102.             }
    103.         /*}*/
    104.     } catch (Exception e) {
    105.         Log.d(TAG, "MainActivity in ClearCache");
    106.     }
    107. }
    I have fragment(ThreeFragment) with button(InternalBtn) that have a context menu. Context menu registered with registerForContextMenu(IntBtn); Menu item ClearCacheMet not working. I cannot see neither Toast.makeText().show(); nor Log.d(TAG, "ClearCache"); Common and ITEM_CANCEL items working. I cannot understand why? Please help me.
    Toast.makeText(getActivity(), "Called !", Toast.LENGTH_SHORT).show(); not called in ClearCache() function. ClearCacheMet and Common were initialized in other fragment.

    public class OneFragment extends Fragment {
    public View OneFragmentView = null;
    public static final int Common = 25;
    public static final int ClearCacheMet = 50;
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    There's no Toast statement in your code. Please clarify what the problem is. If your application crashed, please include the stack trace from the Logcat view.
     
Loading...
Similar Threads - working context menu
  1. ChaunceyK
    Replies:
    0
    Views:
    207
  2. sudz28
    Replies:
    7
    Views:
    284
  3. badmogwai
    Replies:
    7
    Views:
    300
  4. rcyphermd
    Replies:
    2
    Views:
    640
  5. Android All Day
    Replies:
    0
    Views:
    345
  6. Brad Bobak
    Replies:
    5
    Views:
    503
  7. capa wuasta
    Replies:
    10
    Views:
    581
  8. ALL Macs
    Replies:
    2
    Views:
    293
  9. CatGalaxy
    Replies:
    3
    Views:
    235
  10. puppykickr
    Replies:
    6
    Views:
    413

Share This Page

Loading...