0

I have "Activity A" and "Activity B".In "Activity A",bottomnavigation bar is working perfectly fine.Now i need the same bottomnavigation bar of "Activity A" to get worked in "Activity B".I have created the same layout of bottomnavigation bar in "Activity B" and trying to import the functions of "Activity A",but the app is crashing several times.Any help is appreciated.

Activity A:

          switch (item.getItemId()) {
        case R.id.action_picture_from_gallery: {
            if (PermissionChecker.doIfPermissionGranted(this))
            {
                Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                ActivityUtils.get(this).animateToActivity(i, false, REQUEST_LOAD_GALLERY_IMAGE);
            }
            return true;

        }
        case R.id.action_picture_from_camera: {
            showCameraDialog();
            //intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            //startActivityForResult(intent, 9);
            //Toast.makeText(app, "Image From Camera", Toast.LENGTH_SHORT).show();
            return true;
        }


        case R.id.nav_mode_create: {
       Intent i = new Intent(MainActivity.this, ItemsActivity.class);
            startActivity(i);
            //selectedfragment=new HomeFragment();
            break;
        }
        case R.id.nav_mode_favs: {
            _currentMainMode = 1;
            imageList = new ArrayList<>();
            _emptylistText.setText(R.string.no_favourites_description__appspecific);
            for (String fav : app.settings.getFavoriteMemeTemplates()) {
                MemeData.Image img = MemeData.findImage(new File(fav));
                if (img != null) {
                    imageList.add(img);
                }
            }
            _toolbar.setTitle(R.string.favs);
            break;
        }
        case R.id.nav_mode_saved: {
            _currentMainMode = 2;
            _emptylistText.setText(R.string.no_memes_saved_description__appspecific);

            if (PermissionChecker.hasExtStoragePerm(this)) {
                File folder = AssetUpdater.getMemesDir(AppSettings.get());
                folder.mkdirs();
                imageList = MemeData.getCreatedMemes();
            }
            _toolbar.setTitle(R.string.saved);
            break;
        }

        case R.id.nav_mode_hidden: {
            _currentMainMode = 3;
            imageList = new ArrayList<>();

            for (String hidden : app.settings.getHiddenMemesTemplate()) {
                MemeData.Image image = MemeData.findImage(new File(hidden));
                if (image != null) {
                    imageList.add(image);
                }
            }
            _toolbar.setTitle(R.string.hidden);
            break;
        }



        case R.id.nav_more: {

            _currentMainMode = 4;
            _toolbar.setTitle(R.string.more);
            break;

        }


    }

Activity B:

         public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) 
   {
            List<MemeData.Image> imageList = null;
            switch(menuItem.getItemId())
            {
                case R.id.nav_mode_create:
            //Here how can i use method of "Activity A"

                    break;


                case R.id.nav_mode_favs:
       //Here how can i use method of "Activity A"

                    break;

                case R.id.nav_mode_saved:

         //Here how can i use method of "Activity A"
                    break;




                case R.id.nav_mode_hidden:
                   //Here how can i use method of "Activity A"
                    break;

               case R.id.nav_more:
                  //Here how can i use method of "Activity A"
                    break;
            }
            return true;
        }
akshay dhone
  • 43
  • 1
  • 6

2 Answers2

1

It is not good idea to access methods of ActivityA from ActivityB.

If you need to call some common code from both those activities, move that code to other class.

Here is a code sample on Kotlin, but I think it will be clear for you:

public class ActivityA : Activity() {
    lateinit var itemId: Integer

    private val helperClass = YourHelperClass()

    private fun yourActivityAMethod() {
        when (itemId) {
            Integer(1) -> {
                helperClass.methodOne()
            }
            Integer(2) -> {
                helperClass.methodTwo()
            }
            Integer(3) -> {
                helperClass.methodThree()
            }
        }
    }

}

public class ActivityB : Activity() {
    lateinit var itemId: Integer

    private val helperClass = YourHelperClass()

    private fun yourActivityBMethod() {
        when (itemId) {
            Integer(1) -> {
                helperClass.methodOne()
            }
            Integer(2) -> {
                helperClass.methodTwo()
            }
            Integer(3) -> {
                helperClass.methodThree()
            }
        }
    }
}

public class YourHelperClass {

    fun methodOne() {}
    fun methodTwo() {}
    fun methodThree() {}
}
Anton Prokopov
  • 639
  • 6
  • 27
0

Anton has right, helper class would be nice solution. However I'd put the switch into the helper class:

    public class ActivityA : Activity() {
        lateinit var itemId: Integer

        private fun yourActivityAMethod() {
          helperClass = YourHelperClass(itemId)
          helperClass.execute()
        }
    }

//********* Your other Activity

    public class ActivityB : Activity() {
        lateinit var itemId: Integer

        private fun yourActivityAMethod() {
          helperClass = YourHelperClass(itemId)
          helperClass.execute()
        }
    }

//******* Your HelperClass

    public class YourHelperClass(val itemId:Int) {
      when (itemId) {
                Integer(1) -> {
                    methodOne()
                }
                Integer(2) -> {
                    methodTwo()
                }
                Integer(3) -> {
                    methodThree()
                }
            }

        fun methodOne() {}
        fun methodTwo() {}
        fun methodThree() {}
    }
Csongi77
  • 329
  • 3
  • 13