1

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.

Aakash
  • 101
  • 2
  • 11

1 Answers1

2

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);
        ...
    }
}
Sagar
  • 23,903
  • 4
  • 62
  • 62
  • Studio is giving me the warning BaseActivity not registered in the AndroidMainfest.xml. Does i am doing anything wrong ? – Aakash Apr 18 '18 at 13:45
  • you can mark it as `abstract`. This will ensure you don't accidentally run `BaseActivity` stand-alone. See updated answer. – Sagar Apr 18 '18 at 13:49
  • i have learned this in my college but unable to apply the same . Thanks buddy i have understood the same. cheers :) – Aakash Apr 18 '18 at 13:53
  • can we block SS in Fragment also i have fragment also in my app – Aakash Apr 18 '18 at 13:59
  • I think if you apply to host activity, then it should be cascaded to fragment. In case its not, you should be able to do the same with Fragments as you do with Activity i.e. create `BaseFragment` – Sagar Apr 18 '18 at 14:11