Use .getWindow() in Application-file - java

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

Related

How to show dialog when app is in background in android app like collect app

image
I am sharing a link from another app like youtube to my app but Now it opens my app and then saves the link. My need is to not save the link without opening the app
There are two solutions:
You don't need dialog or anything like it. Just make the activity fully transparent, process data and finish it. Optionally, you can show Toast as a feedback. It would look and feel like everything is processed in the background.
You need dialog or some kind of UI. For this, you can use floating windows that you can show over other apps. Many apps use this approach such as Pocket, etc. There is complete guide on how to do it: https://localazy.com/blog/floating-windows-on-android-1-jetpack-compose-room

Fresh loading activity every time of calling it

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.

Launch an application in a different "context"

First of all, sorry for the "context" in the question title, I didn't know which word to use. I successfully launch my app by clicking over a URL from another application, but when I launch the task manager I realize that my application is not actually loaded: the caller app holds the activities. I would like how to:
Launch my app in a different "context" (sorry for the word again, which would be better?)
Be able to reload my app in the case it was already loaded (something like restarting it).
Thank you so much.
You can modify the behavior by setting "launchMode" attribute in AndroidManifest.xml to either "singleTask" or "singleInstance", both would cause your Activity to be created as the root of a new task. However it doesn't restart the Activity if it exist already, instead you should handle the Activity.onNewIntent(Intent intent) callback.
To learn more on launchMode see here: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
In addition to launchMode that Kai mentioned, you might also want to look at taskAffinity and allowTaskReparenting, depending on how your app is structured.
This is an excerpt from there that seems to match how you describe your app:
For example, if an e-mail message contains a link to a web page, clicking the link brings up an activity that can display the page. That activity is defined by the browser application, but is launched as part of the e-mail task. If it's reparented to the browser task, it will be shown when the browser next comes to the front, and will be absent when the e-mail task again comes forward.

First android app for iOS developer

i am iOS app developer. Now when i created an app on iOS i want to do the same on android. With java i was familiar just now need some time to remember.
i was looking at some tutorial how to do one or other things. But what i can't find is the basics how everything works. How classes is interacting with each other.
For example i want to create registration window with few buttons and alerts.
I want registration window to be called just once when app is installed and just that.
Should i create new java class and few layouts, one for View with buttons and other for Alerts ?
Or should i create other class for alerts if i need them in other flow of my app ?
And how i should call that window with registration from my main class, which is
extends Activity
Also if there are some developers who came this road from objective-c (iOS) to java (android). It would be nice for some share experience how they did that.
Thank you for any help :)
Very few of the concepts in iOS and Android are similar. On Android you have Activites, Intendts the Manifest. When you design your layout it should be resolution independent. You have a search, back and a menu button and variable hardware. All of this has no equivalent in iOS.
That said, I think you just have to read the basic concepts and the getting started guide no matter if you come from iOS or never have done mobile development before.
EDIT
To answer your concrete question. Take a look at the lifecycle of an Activity and Preferences. With this, you could do some action on the first start of your main Activity and store some flag in the preferences when it's done. On the next start you just test that preference and skip the logic.
You can create one activity (.java file) and one layout(.xml file with buttons and input boxes) , alerts could be toast notifications:
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
All you require for this is a activity and a layout xml for that activity, this activity will be your main ie the entry point to your application, in that activity oncreate method you can check if it is registered or not by setting a flag or something which will direct to the next activity if its registered.
GOOD LUCK...
Just like your nibs in iPhone you create xml layouts in Android. And for view controllers here you make activity. One important thing is AndroidManifest.xml file, it contains all information of your app (like plist) plus all the activity information(Intent type and launcher methods).

How to prevent Android application from loading multiple times?

This must be an easy one but I'm not having much luck searching for an answer.
Apologies if this is a regular question.
If I navigate away from my app I cannot return to it. Starting the app again will load a second instance of it rather than returning to it. If I leave an audio loop running in my app, It's hard to get back in and turn it off.
On startup I'd like the app to destroy any previous instance of itself left running.
I'd also like to try having the app shut itself down when I navigate away (I know it's not the right way to do things but I'd like to try this for my own personal use of the app). Or have the "back" button destroy the app.
Thanks.
Add this to your activity definition in manifest...
android:launchMode = "singleInstance"
How to prevent the activity from loading twice on pressing the button
I have answered such a question,you can declare android:launchMode="singleTask" attribute for your MainActivity in the AndroidManifest.xml file, so that the OS won't create a new instance if there is one running in the background.

Categories