Nothing shows up while calling a method - android

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.

Related

two build types - duplicate code. Not good solution

Android Studio 3.4
I have the next activity:
public class CartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod()
}
private void someCommonMethod() {
// some code
}
}
Now I have 2 build types: debug and release.
In debug I add method someDebugMethod() to activity
public class CartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod()
someDebugMethod()
}
private void someCommonMethod() {
// some code
}
private void someDebugMethod() {
// some debug code
}
}
In release I add method someReleaseMethod() to activity
public class CartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod()
someReleaseMethod()
}
private void someCommonMethod() {
// some code
}
private void someReleaseMethod() {
// some debug code
}
}
I read official documentation
and now project's stucture is:
app\src\debug\java\com\myproject\CartActivity.java
app\src\release\java\com\myproject\CartActivity.java
I remove CartActivity.java from app\src\main\java.
So, as a result, I have two files CartActivity.java.
Nice.
And now when I start the app in debug build type then runCartActivity in debug folder.
And when I start the app in release build type then runCartActivity in release folder.
Nice. It's work fine.
But suppose now I need to update the common method someCommonMethod(). This method used in both build types.
As a result, I need to update TWICE this method. First in app\src\debug\java\com\myproject\CartActivity.java and
then update same method with same code in app\src\release\java\com\myproject\CartActivity.java
So I think this is not good.
Because this is duplicate code. I need to copy & paste EVERY time in TWO files when update method someCommonMethod().
It's really bad.
How to avoid this duplicate code?
The ideal approach is when in CartActivity.java has only delta.
In app\src\debug\java\com\myproject\CartActivity.java has ONLY method someDebugMethod()
In app\src\release\java\com\myproject\CartActivity.java has ONLY method someReleaseMethod()
and common code is in app\src\main\java\com\myproject\CartActivity.java
Is it possible?
P.S. Suppose I have 3 build types.
As result, I need to update same code in three files. It's really not good.
Just create a CommonCartActivity in
app\src\main\java\com\myproject\CommonCartActivity.java
Then extend the class overriding the methods in the flavor implementations.
public class CartActivity extends CommonCartActivity {
protected void someCommonMethod() {
// some code
}
}
In this way CartActivity just inherits from CommonCartActivity with nothing else, duplicated in two flavors.
I think you do not need to complicate the structure of the project. Just use the real-time check.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod();
if(BuildConfig.DEBUG){
someDebugMethod();
}else{
someReleaseMethod();
}
}
private void someCommonMethod() {
// some code
}
private void someDebugMethod() {
// some debug code
}
private void someReleaseMethod() {
// some release code
}
But, if you need to separate the code, try using one activity, but create a new class (for example, Fork) in two copies for release and debug. Create an instance of the class in the activity. In this class there will be a doMetnod() which will have the necessary code depending on the type of project. So you avoid duplication of the activity code.

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

Butterknife not detecting the source of event

I have just started working with Butterknife library and written the following code:
class myActivity extends AppCompatActivity
{
#BindView(R.id.button) Button app1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
public void selectApp(View b)
{
Button clicked=(Button)b;
if(clicked==app1)
Toast.makeText(this,"First App clicked",Toast.LENGTH_LONG).show();
}
}
here selectApp is attached through onClick in the xml view file.
But the problem is clicked==app1 is returning false even when pressing app1. The method is being called but the if condition is coming false.
Can anybody clarify.
Thanks
I think this would work:
if(clicked.getId()==R.id.button)
Also, you can use View b and not parse into a button:
if(b.getId()==R.id.button)
¿Is this your actual code? seems to lack an annotation on the method.

Android return to app call method

I just recently started learning how to build android apps, and encountered a problem:
I want, when users leave the app (go to the homescreen, multitask), and they return, that the app calls a certain method. How can I do that?
This problem is more tricky than it may look like. When you return to app after leaving it, then is called method onResume of activity which was active when app was interrupted. But same happens when you go from one activity to another (onResume of second activity is called). If you just call method from onResume, it will be called every time onResume of any activity is called.
Take a look at this solution...
First, you have BaseActivity which is extended by all activities that need to call that method:
abstract public class BaseActivity extends Activity implements IName {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected void onResume() {
if (AppClass.getPausedActivity() != null) {
if (this.getClassName().equals(AppClass.getPausedActivity()))
//call specific method
}
AppClass.setPausedActivity("");
super.onResume();
}
#Override
protected void onPause() {
AppClass.setPausedActivity(this.getClassName());
super.onPause();
}
#Override
abstract public String getClassName();
}
As you can see it implements interface IName:
public interface IName
{
String getClassName();
}
BaseActivity in onPause (when it is interrupted) calls setPausedActivity method of AppClass which remembers last activity name that was interrupted. In onResume (when app and activity is continued) we compare name of current activity and last paused activity.
So, when app is interrupted, these names will be same because you paused one activity and you got back to the same one. When you call activity from some other activity these names will not be same and method will not be called.
Here is code for AppClass:
public class AppClass extends Application {
public static String pausedActivity;
public static String getPausedActivity() {
return pausedActivity;
}
public static void setPausedActivity(String _pausedActivity) {
pausedActivity = _pausedActivity;
}
}
Also, here is example of activity that extends BaseActivity:
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
}
//here you set name of current activity
#Override
public String getClassName() {
return "MainActivity";
}
}
You are bound to the Activity lifecycle. You will need to implement corresponding logic to figure out if the user has been in your app before (i.e. using SharedPreferences).

Android application closes on click

I do my android app , but I have a bug I don't know how to fix it.
My code is below :
This is my Main Activity :
public class MainActivity extends Activity {
private ImageView imgHot;
public final static String EXTRA_MESSAGE="com.cuonglm.KhoHinh.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgHot=(ImageView)findViewById(R.id.imageViewHot);
imgHot.setOnClickListener(toContentHot);
}
View.OnClickListener toContentHot=new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent content=new Intent(MainActivity.this,ContentActivity.class);
String signal="1";
content.putExtra(EXTRA_MESSAGE,signal);
startActivity(content);
}
};
And this is my second Activity :
public class ContentActivity extends Activity {
private TextView viewMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
setContentView(R.layout.activity_content);
}
I want to click on the image on the Main Activity , string "1" or number "1" will send to the Second Activity via Intent and view on the TextView.
But my app will be close "Unfortunately..."
Thanks
Change to
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
In your ContentActivity
findViewById looks for a view with the id for the current infalted layout. SO you need to set the content of your layout to the activity first and then initialize your views.
You are probably gettting NullPointerException coz your initialization fails.
You need to call setContentView() in your second Activity before trying to access any of the Views in that layout. Change it to
public class ContentActivity extends Activity {
private TextView viewMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
}
If this doesn't fix your problem then please post your logcat so we can see the error. Also always post logcat in the future when your app crashes. They aren't always this easy to see.
Also, I'm not sure you understand how putExtra() works. It is a key, value pair so when you put EXTRA_MESSAGE as the key then that is what you would use to retrieve the value added in the second param. So the way you are doing it may work if the Activity gets destroyed but it looks really strange to me and probably not realy safe or efficient. I would change it to something like
content.putExtra("message",signal);
in your first Activity then get it with
String messageReceive = content.getStringExtra("message");
in your second Activity
You need to set the layout before trying to reference the Views associated with it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
setContentView(R.layout.activity_content);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
}

Categories

Resources