Fresh loading activity every time of calling it - java

In an android App im showing advertisement from a local advertisement company.
My problem is when start App and close it with back button, and back to it by clicking on its icon, i see the same Ad. But when i close the app from android task manager and back to the app, i see another Ad.
I know this can have multiple reasons and my question is not clear, But my main questions that how i can set each activity runs like first time of running the app? What should cause this reaction? its like a cookie system... i want every activity show new Ad!
Here you can see example code:
http://github.com/adad-project/client-app-sample

Move
Adad.initialize(getApplicationContext());
((AdView) findViewById(R.id.banner_ad_view)).setAdListener(mAdListener);
to onStart and remove from onCreate in MainActivity
Do similar thing in other activities.

Related

How to refresh activity while not using the app?

I'm working with realtime database where I want my activities to run even no one is using the app.
swipeRefresh.setOnRefreshListener {
// code
binding.swipeRefresh.isRefreshing = false
}
From the above code, the user needs to swipe to refresh, I want it to be the same but in background and it does itself.
That means the activity is like a 'refresh' in the background every five seconds.
It's not possible to keep an app running while it's not being used. Android app processes will be killed by Android when the user is no longer using the app, and this can't be prevented. Android does this to save resources and allow other apps to run when they are being used. I suggest reading the documentation to understand how it works.
Your app should isntead simply query the database again when it's launched and rebuild the UI.

Use .getWindow() in Application-file

This may be a silly question but I have the following situation: I want to setup my window layout every time the app is open, for example changing the status bar color. So I created my Application file because I heard it is better if you check something like this in the application file and not in the MainActivity.
Problem: How can I call the getWindow() method without an open activity.
Thanks for helping.
A bit tricky, but you can use Window manager in background-service to display your views without opening an Activity.
Also, for API Level 26+ (Oreo or above) you have to start your service as foreground service

Is there a way to put a running android app as the android device wallpaper?

In detail, I want to know if it's possible to take the output from a running Android app and pipe it to the to the background of the display. In other words, have the background wallpaper update itself dynamically.
No you can't do it this way. The reason is because Android starts your app with an activity (unless your app is just made up of services to run in the background for other apps to call). Whenever your activity starts, it comes to the foreground and receives user focus. The Android documentation states that if the user performs an action that starts another activity or switches to a different app, the system calls another set of lifecycle methods on your activity as it moves to the background. At this point, the activity is no longer visible.
The closest concept to what you are trying to do is a live wallpaper but that is a service and is a different paradigm.

Preventing the creation of a new instance of an Activity

I am developing an android music player app and i noticed that when i press the home button and open the app again it takes me to a new instance of the main activity, even when the music is playing.
My question is, is there any way i can prevent this from happening? I want to make it so that when pressing the application icon after it is already open, the first instance of the main activity is reopened without reloading?
I tried changing the launchMode on the manifest, as it says here:
http://developer.android.com/guide/topics/manifest/activity-element.html
Many thanks :)

Creating a login type of activity and forcing it the only opening point

I have been doing my research, but I feel as if I am missing something.
I have an app with a login. Each time you open the app, you should be forced through that login page. You should never be able to resume onto any activity other than the login.
In the manifest I have
android:clearTaskOnLaunch="true"
on the main activity I wish to use as the login activity,
and
android:finishOnTaskLaunch="true"
as well as
android:excludeFromRecents="true"
on the rest of the activities.
The problamatic situation occurs when you go from the login to another activity, hit home, and relaunch the app via the icon. It should jump back to the login page, but it doesnt. Any idea?
I have also been installing as a regular apk, not via eclipse as I know that there is an issue with eclipse and some of the manifest attributes.
Perhaps if there is a way to detect that the activity launch came from the app icon press, I could manage it that way, but I dont think that is possible either.
In either onResume or onRestart you could check a series of flags, such as a login timeout, then force the user back to the login activity using an Intent, while at the same time finishing the original activity.
I like this method in favor or just finishing the app in either onPause or onStop because it gives you a chance to make some checks before blindly closing the application.
Or, you could try using the android:noHistory tag in your manifest file.
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
There are also other tags such as, android:finishOnTaskLaunch
Whether or not an existing instance of the activity should be shut down (finished) whenever the user again launches its task (chooses the task on the home screen) — "true" if it should be shut down, and "false" if not. The default value is "false".
More information here: http://developer.android.com/guide/topics/manifest/activity-element.html
The easiest and fastest way is probably to terminate the activity with finish(); in its onPause() since this is invoked when the App is put into the background.
It might be possible to solve with XML configuration as well, but not that I know of by heart.

Categories