0

我有“活动 A”和“活动 B”。在“活动 A”中,底部导航栏工作得非常好。现在我需要与“活动 A”相同的底部导航栏才能在“活动 B”中工作。我已经创建了“Activity B”中底部导航栏的布局相同,并尝试导入“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;

        }


    }

活动 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;
        }
4

2 回答 2

1

从 ActivityB 访问 ActivityA 的方法不是一个好主意。

如果您需要从这两个活动中调用一些公共代码,请将该代码移至其他类。

这是 Kotlin 上的代码示例,但我认为这对您来说很清楚:

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() {}
}
于 2019-10-02T13:04:37.243 回答
0

安东是对的,助手类将是很好的解决方案。但是我会将开关放入助手类:

    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() {}
    }
于 2019-10-02T13:27:48.487 回答