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

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.

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);
...
}
}

Related

Nothing shows up while calling a method

I was developing an exam score calculator app.
When I want to call AD methods,advertisements don't show up.
Calculation process happens in OnCreate method:
public class resultActivity extends AppCompatActivity {
public String responseId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
/*Calculation...*/}
and other voids like:
public void requestAd() {
/*AD RQUESTING PROCESS...*/
}
and
public void showAd() {
/*AD SHOWING PROCESS...*/
}
AD team gave me this code to call the method and it works well:
requestButton.setOnClickListener(v -> requestAd());
showButton.setOnClickListener(v -> showAd());
But the Problem is I don't have buttons to call them so I tried this:
public class resultActivity extends AppCompatActivity {
public String responseId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
requestAd();
showAd();
/*Calculation...*/}
But when the activity starts ads don't show up!
The whole question is I want this methods to be called while this activity starts.
thank you.
Try building up the release version APK and test on it. Maybe your Ad-provider have some restrictions in debug version?
I made another class and moved request Ad and showAd there. Then, I made an object and called the method through object.
I have to mention that I changed a minor thing in requestAd but the main job was done by the object.
Thank You All.

What is the use of Android static inner activity class and where exactly we can use?

I have created a Activity called MainActivity Which extends from ThirdActivity.
Next, I have an inner static activity called AnotherActivity which extends from SecondActivity. Below is my code:
1)MainActivity: which is the first activity that calls at first when app loads.
public class MainActivity extends ThirdActivity {
public static class AnotherActivity extends SecondActivity{
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("Main Activity");
}
}
2) ThirdActivty Code follows:
public abstract class ThirdActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
System.out.println("Third Activity");
}
}
3) Inner static activity extended from SecondActivity which consists of:
public abstract class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
System.out.println("Second Activity");
}
}
Now the output of the above android program is:
Third Activity
MainActivity
In order to access the inner activities we need to do declare in manifest as:
<activity android:name="MainActivity$AnotherActivity" ></activity>
So, My question goes like this:
1) How to access Static inner Activity classes.
2) When to use this scenario.?
3) Will the inner activity runs the life cycle process and what happens to already running MainActivity?
I love answering bizarre questions before having coffee.
1) How to access Static inner Activity classes
What do you mean by "access"? The fact that this is a static inner class is pretty much irrelevant. It will run like an normal Activity. When it is running, its onCreate method, e.g., will be called. When that happens, there is a reference to the running instance of AnotherActivity in the canonical variable this.
2) When to use this scenario.
I can't think of a single reason. In fact, I can think of a million reasons not to use it. Ever.
3) Will the inner activity runs the life cycle process and what
happens to already running MainActivity?
Yes. The "inner" activity will run through a normal Activity lifecycle. Nothing happens to the MainActivity because it is not running when the AnotherActivity is running. When AnotherActivity is started, MainActivity is stopped (onPause, maybe onStop, etc).

Call method from other class

Ok I am a complete newb when it comes to java classes. I have a public method that dynamically displays some Linearlayouts with some stuff in them. For instance this method (public void methodA)is in ClassA.java, then I want to call methodA from inside ClassB.java. Both of the classes extend Activity and the methodA is being called in the OnCreate method.
ClassA.java
public class ClassA extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
methodA();
}
public void methodA() {
//Do Stuff
/* This uses:
* Package Manager
* Buttons using(this)
* Linear Layouts using(this)
* TextViews using(this)
* findViewById()
* startActivity
*/
}
}
ClassB.java
public class ClassB extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
methodA(); //How do I do this
}
}
It is clear to me that the method is specific to the first activity, which means that it shouldn't be called in the second one. You can either reimplement the method in the second activity or, if these activities are similar (don't do this if they aren't!), you have two options:
Inherit the second activity from the first one.
Merge these two activities into one and use different intents to launch them and act accordingly.
The second method is easier to maintain, so I would prefer it over the first one in simpler cases.
Craete instance of class A in class B and then you can invoke MethodA from class B
public class ClassB extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//MethodA(); //How do I do this
ClassA a=new ClassA ();
a.MethodA();
}
}

Use a container for Android activities

I want to know if Android support the possibility to make a container with some static components and just include my activities in it.
I have a sliding menu with some onClickListener events and I don't want to set these events for each activity.
If I understood you correctly, you have some functionality that is common to several Activities, and you don't want to repeat the same code in all of them. Instead, you want to do that in one place.
One way to achieve this is to create a superclass activity, place your common code in that activity, and then extend it with your other activities. For example,
public class BaseActivity extends Activity implements OnClickListener {
private Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.my_button);
button.setOnClickListener(this);
}
...
#Override
public void onClick(View view) {
int id = view.getId();
switch(id) {
case R.id.my_button:
// perform action
break;
}
}
}
Then you extend it as
public class Activity1 extends BaseActivity {
/...
}
public class Activity2 extends BaseActivity {
/...
}
public class Activity3 extends BaseActivity {
/...
}
I am not exactly sure I understand your question, can you perhaps elaborate some more? maybe even post some sample code that you are using currently.
From what I can tell you should be able to achieve what you want by making your own CustomActivity
public class CustomActivity extends Activity {
//put your slidingmenu stuff here
}
Then inside all of the other Activities where you want to use that shared piece do it like this:
public class AnotherActivity extends CustomActivity {
//...
}
with extends CustomActivity instead of the usual extends Activity
This is how i solved the problem:
First thing i did is creating my main class wich will host common code.
for example :
public abstract class main extends activity(){
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
// Your common code here
}
protected abstract int getLayoutResourceId();
}
Then all what you need is to extend this class in your activity:
public class HelloActivity extends main{
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_hello);
super.onCreate(savedInstanceState);
//make sure to put setcontentview before super.oncreate
}
#Override
protected int getLayoutResourceId() {
return R.layout.activity_hello;
}
}
All the activities needs to be registered in the Manifest. For the common things, e.g. Slide menu, you can use Fragment for the slide menu.

How do we get the theme of an Activity?

I am trying to detect the theme used for an activity but so far I am only able to find that out for the entire application? Is there a way to do this?
Just call the method getTheme() in your activity. For example.
public class MainActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Theme theme = this.getTheme();
}
}
My first thought would be ContextThemeWrapper.getTheme(), since Activity extends ContextThemeWrapper

Categories

Resources