1. Download our Official Android App: Forums for Android!

Not working context menu item in fragment

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

  1. masterpda13

    masterpda13 Lurker
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    8
    Joined:
    May 14, 2017

    May 14, 2017
    8
    3
    16
    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;
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,827
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,827
    11,398
    1,988
    Male
    Software developer
    South West of England
    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.
     

Share This Page

Loading...