Search code examples
androidandroid-securityandroid-application-class

How to prevent the Screentshot in the entire android app without repeating the same code


Hi everyone i want to block the Screenshot in my app. I got the first problem solve from here.

But now the thing is I have more than 10 activity and 10 + fragment.

Is there any way to do this just by writing in the one class and giving it reference to the entire app.

Just like we make one Application class and in the AndroidMainfest.xml give that application class refrence.


Solution

  • You can implement a BaseActivity, and make all your activities extend this BaseActivity. In onCreate() of this activity set the flag. You need to ensure all your activities call super.onCreate() as follows:

    BaseActivity.java

    public abstract class BaseActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //set your flag here
            ...
        }
    }
    

    Activity1.java

    public class Activity1 extends BaseActivity{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ...
        }
    }