Как вызвать кнопку из другой кнопки в другом классе? - PullRequest
0 голосов
/ 24 октября 2019

У меня есть кнопка в моей MainActivity. Я хочу вызвать ее в своей SecondActivity внутри другой кнопки, в основном обе кнопки делают одно и то же

MainActivity

 share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Drawable myDrawable = scannedImageView.getDrawable();
            Bitmap bitmap = ((BitmapDrawable)myDrawable).getBitmap();
            try{
                File file = new File(MainActivity.this.getExternalCacheDir(), "myImage.png");
                FileOutputStream fOut = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 80, fOut);
                fOut.flush();
                fOut.close();
                file.setReadable(true, false);
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                intent.setType("image/png");
                startActivity(Intent.createChooser(intent, "Share Image Via"));
            }catch (FileNotFoundException e){
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "File not found", Toast.LENGTH_SHORT).show();
            }catch (IOException e){
                e.printStackTrace();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });

SecondActivity

 share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //Calling button from MainActivity
        }
    });

1 Ответ

1 голос
/ 24 октября 2019

создайте статический метод в классе utils для примера, который возвращает OnClickListener

public static View.OnClickListener getShareButtonClickListener(Activity activity) {
    return new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Drawable myDrawable = scannedImageView.getDrawable();
            Bitmap bitmap = ((BitmapDrawable)myDrawable).getBitmap();
            try{
                File file = new File(activity, "myImage.png");
                FileOutputStream fOut = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 80, fOut);
                fOut.flush();
                fOut.close();
                file.setReadable(true, false);
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                intent.setType("image/png");
                activity.startActivity(Intent.createChooser(intent, "Share Image Via"));
            }catch (FileNotFoundException e){
                e.printStackTrace();
                Toast.makeText(activity, "File not found", Toast.LENGTH_SHORT).show();
            }catch (IOException e){
                e.printStackTrace();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    };
}

и в обоих действиях

share.setOnCLickListener(MyUtilsClass.getShareButtonClickListener(this))

но я согласен с @Abbas, лучшеразделить ваш взгляд (деятельность / фрагменты) и вашу бизнес-логику

...