Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Splinter
Jul 4, 2003
Cowabunga!
What's the practical difference between Fragment onCreateView and onActivityCreated? I've seen some examples were initialization code is split between these two methods, but I'm having a hard time determining when something must wait for onActivityCreated verses when it won't make a difference either way.

Adbot
ADBOT LOVES YOU

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

Splinter posted:

What's the practical difference between Fragment onCreateView and onActivityCreated? I've seen some examples were initialization code is split between these two methods, but I'm having a hard time determining when something must wait for onActivityCreated verses when it won't make a difference either way.

A common pattern involves referencing the parent activity to get your data model (via injection), so while your views can be all set up and ready you can't do anything with them yet except set default values.

Ensign Expendable
Nov 11, 2008

Lager beer is proof that god loves us
Pillbug
I've been out of the Android game for a long time and I've been hoping to get back to it. Is there a good list of new stuff that was added in the last 2-3 years? I hear Kotlin is all the rage these days, for instance. Is there a good guide on how to switch to for a Java dev? Is switching even worth it?

kitten smoothie
Dec 29, 2001

Ensign Expendable posted:

I've been out of the Android game for a long time and I've been hoping to get back to it. Is there a good list of new stuff that was added in the last 2-3 years? I hear Kotlin is all the rage these days, for instance. Is there a good guide on how to switch to for a Java dev? Is switching even worth it?

I've heard good things about the new Big Nerd Ranch Kotlin book, or Antonio Leiva's Kotlin guide.

Thanks to the interop with Java, "switching" really just means "write Kotlin for new stuff, and convert Java to Kotlin at your leisure." It's definitely worth it, I find the language to be a joy to work with. Being able to lean more on the compiler has been awesome for reducing NPE-related crashes.

Other things to check out if you've been out of the loop
- architecture components (there's a lot of helpful stuff with respect to persistence and lifecycle management)
- read up on the gradle plugin tooling because a lot's changed
- background processing rules have changed in Android 8 and might bite you, so definitely get caught up on that

kitten smoothie fucked around with this message at 18:26 on Aug 20, 2018

Vesi
Jan 12, 2005

pikachu looking at?
The sunflower sample app is a good starting point for new apps I think, it has all the latest things being used as they're supposed to

https://github.com/googlesamples/android-sunflower

Ensign Expendable
Nov 11, 2008

Lager beer is proof that god loves us
Pillbug
Thanks, I'll check it out. Hopefully I'll be ruining the Awful app in no time.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

kitten smoothie posted:

- background processing rules have changed in Android 8 and might bite you, so definitely get caught up on that

This one is enormous. Background services as they were are effectively gone. You can request to be scheduled at a certain time or with certain criteria (when the network returns etc) but the time at which you run may be drastically different from when you expect to run, and if the user doesn't interact with you that might eventually turn into "never".

Tragedy of the commons and all that but it's disappointing.

kitten smoothie
Dec 29, 2001

Then some third rate OEMs (Xiaomi, Huawei, Oppo) gently caress with their underlying JobScheduler implementations to further "optimize" battery by not letting anything run at all or randomly killing things.

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Ensign Expendable posted:

Thanks, I'll check it out. Hopefully I'll be ruining the Awful app in no time.

lol if you think we're using the new stuff :frogbon:

speaking of, anyone used the new Navigation bits? I just wrote my own navigation system for the app that passes event types around, routes them up to the activity and converts to/from Intents, so it'll route where it needs to go and launch the relevant activity if necessary. I like the look of the Navigation library (especially the GUI for it and the backstack generation) but the boilerplate

Java code:
override fun onClick(v: View?) {
   val amountTv: EditText = view!!.findViewById(R.id.editTextAmount)
   val amount = amountTv.text.toString().toInt()
   val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
   action.amount = amount
   v.findNavController().navigate(action)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val tv: TextView = view.findViewById(R.id.textViewAmount)
    val amount = ConfirmationFragmentArgs.fromBundle(arguments).amount
    tv.text = amount.toString()
}

quote:

When you generate code using the safeargs plugin, simple object and builder classes are created for the action and sending and receiving destinations. These classes are:

  • A class for the destination where the action originates, appended with the word "Directions".

    So, if an origin fragment is titled SpecifyAmountFragment, the generated class is called SpecifyAmountFragmentDirections. This class has a method, named for the action used to pass the argument, to bundle the argument, such confirmationAction().

  • An inner class whose name is based on the action used to pass the argument. If the passing action is called confirmationAction, the class is named ConfirmationAction.

  • A class for the destination where the arguments are passed, appended with the word Args.

    So, if the destination fragment is titled ConfirmationFragment, the generated class is called ConfirmationFragmentArgs. Use this class's fromBundle() method to retrieve the arguments.

I might have tunnel vision from doing my own but this feels unwieldy compared to navigate(NavigationEvent(paramsForThisEventType)) and val event: NavigationEvent? = intent.parse(). I'm happy to rework it if it fits but it doesn't feel great

Splinter
Jul 4, 2003
Cowabunga!

Volmarias posted:

A common pattern involves referencing the parent activity to get your data model (via injection), so while your views can be all set up and ready you can't do anything with them yet except set default values.

Ahhhh, I think what I was missing was in some cases Activity.onCreate finishes before Fragment.onCreateView is called, but in many other cases that isn't guaranteed (at least according to this). Thanks!

~

I'm writing an app using LiveData and Room, and am wondering if anyone has thoughts on the best way to leverage DAO queries that return LiveData when working in a non-lifecycle or background thread situation. Specifically, I have a JobIntentService triggered by the AlarmManager that I use to periodically send notifications. Previously I had a hacky solution using observeForever() to get the LiveData, then immediately removing the observer:

code:
LiveData<List<Task>> observableTasks = ((MyApp) getApplication())
        .getRepository()
        .loadOutstandingTasksWithNotifications();

Observer<List<Task>> observer = new Observer<List<Task>>() {
    @Override
    public void onChanged(@Nullable List<Task> tasks) {
        if (tasks != null && !tasks.isEmpty()) {
            // do stuff with tasks
        }
        observableTasks.removeObserver(this);
    }
};

observableTasks.observeForever(observer);
However, calling observeForever() on a background thread is no longer allowed (believe this is part of version 2 of the lifecycle extensions). An easy solution is to add another DAO method using the same query that returns a List instead of LiveData, but it seems smelly to me to have to duplicate any queries that need to be run in both lifecycle and background situations. One idea I had is to have separate repository subclasses for LiveData and non-LiveData (to avoid having to have all method names clarify whether or not LiveData is returned), but I'm not in love with that approach. Anyone have any thoughts on how to approach this, and/or is there a standard or straightforward way to handle this that I'm missing?

Vesi
Jan 12, 2005

pikachu looking at?

baka kaba posted:

Java code:
override fun onClick(v: View?) {
   val amountTv: EditText = view!!.findViewById(R.id.editTextAmount)
   val amount = amountTv.text.toString().toInt()
   val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
   action.amount = amount
   v.findNavController().navigate(action)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val tv: TextView = view.findViewById(R.id.textViewAmount)
    val amount = ConfirmationFragmentArgs.fromBundle(arguments).amount
    tv.text = amount.toString()
}
I might have tunnel vision from doing my own but this feels unwieldy compared to navigate(NavigationEvent(paramsForThisEventType)) and val event: NavigationEvent? = intent.parse(). I'm happy to rework it if it fits but it doesn't feel great

You have an unnecessary assignation there, you can just

Java code:
override fun onClick(v: View?) {
   val amountTv: EditText = view?.findViewById(R.id.editTextAmount) ?: return
   v.findNavController().navigate(SpecifyAmountFragmentDirections.confirmationAction(amountTv.text.toString().toInt()))
}

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

i think u mean
Java code:
override fun onClick(v: View?) {
    v?findViewById(R.id.editTextAmount)?.run { text.toString().toInt().run(SpecifyAmountFragmentDirections::confirmationAction).run { v.findNavController().navigate(this) } }
}
zero variables, we did it!

That was their example code though, I guess they were trying to make it readable. Plus we skipped a bit, you need to pass in the amount with val action = SpecifyAmountFragmentDirections.confirmationAction(amount) and then assign it again with
action.amount = amount for some reason? Maybe that's a mistake because the Java version doesn't pass it in when it gets the action :shrug:

PokeJoe
Aug 24, 2004

hail cgatan


Splinter posted:

Ahhhh, I think what I was missing was in some cases Activity.onCreate finishes before Fragment.onCreateView is called, but in many other cases that isn't guaranteed (at least according to this). Thanks!

~

I'm writing an app using LiveData and Room, and am wondering if anyone has thoughts on the best way to leverage DAO queries that return LiveData when working in a non-lifecycle or background thread situation. Specifically, I have a JobIntentService triggered by the AlarmManager that I use to periodically send notifications. Previously I had a hacky solution using observeForever() to get the LiveData, then immediately removing the observer:

code:
LiveData<List<Task>> observableTasks = ((MyApp) getApplication())
        .getRepository()
        .loadOutstandingTasksWithNotifications();

Observer<List<Task>> observer = new Observer<List<Task>>() {
    @Override
    public void onChanged(@Nullable List<Task> tasks) {
        if (tasks != null && !tasks.isEmpty()) {
            // do stuff with tasks
        }
        observableTasks.removeObserver(this);
    }
};

observableTasks.observeForever(observer);
However, calling observeForever() on a background thread is no longer allowed (believe this is part of version 2 of the lifecycle extensions). An easy solution is to add another DAO method using the same query that returns a List instead of LiveData, but it seems smelly to me to have to duplicate any queries that need to be run in both lifecycle and background situations. One idea I had is to have separate repository subclasses for LiveData and non-LiveData (to avoid having to have all method names clarify whether or not LiveData is returned), but I'm not in love with that approach. Anyone have any thoughts on how to approach this, and/or is there a standard or straightforward way to handle this that I'm missing?

I had the same issue with LiveData and built the exact same hacky immediately remove the observer thing as you, so I'd be interested in other approaches to this. What I ended up doing was moving back to Rx :shrug:

FateFree
Nov 14, 2003

Hey guys, I have an old android app I wrote years ago for a client and I got an email saying: Update your Android targetSdkVersion by November 1st to 26 or something and mine was written for 15. But i wrote this in eclipse and I don't even think thats a supported IDE anymore, also I hate android development. How much of a pain in the rear end is the upgrade path going to be? Is this something I could easily outsource to Upwork or something?

PokeJoe
Aug 24, 2004

hail cgatan


Most people I know personally in your position have just abandoned their old apps. Sometimes the swap to Android studio is painless and sometimes it's a sisyphean nightmare, it just depends on your specific project. Maybe just give it a try and timebox yourself to not get carried away with it.

kitten smoothie
Dec 29, 2001

You don't need to bump the target SDK version until you submit an update. They haven't said they'll issue takedowns or policy strikes for failure to update the version, they're just blocking new APK uploads.

Eclipse and the ADT have been unsupported since summer 2015, so I'm guessing it's been at least three years since you've last touched this. If you haven't touched it since then, are you ever going to? I'm betting strong no.

FateFree
Nov 14, 2003

Oh believe me I never want to touch it again, but a client paid me to write it and they don't want it to be taken down on November 1st. But.... if Google won't touch a running one then maybe I can get off scott free by never updating again!

kitten smoothie
Dec 29, 2001

FateFree posted:

Oh believe me I never want to touch it again, but a client paid me to write it and they don't want it to be taken down on November 1st. But.... if Google won't touch a running one then maybe I can get off scott free by never updating again!

https://android-developers.googleblog.com/2017/12/improving-app-security-and-performance.html

quote:

Existing apps that are not receiving updates are unaffected.

There ya go, just carry on with your previous proven "never update" strategy and you're good to go.

That said, while it won't get taken off the store, Android 9+ will probably present a warning though, as AFAIK it will start to inform the user "this is designed for an older version of Android" when installing apps that target 16 or lower.

kitten smoothie fucked around with this message at 02:06 on Sep 20, 2018

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
Yeah, you should mention to your client that they should eventually prepare to pay someone to handle an upgrade. The simpler the app and less custom the UI, the less work it's likely to be.

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
Anyone have any experience using Unity for Android, or alternately, any kind of AR, with extensive camera use?

I'm currently having major battery drain problems, which I think are largely due to the camera, but I haven't been able to nail it down. Android itself claims the app is using almost zero battery. Obviously, the screen being on contributes to this, but not nearly enough to cause the numbers I'm seeing.

It's definitely not a graphically or CPU intensive app. I'm not super sure where to start looking. Not much has come up through search.

Shadow0
Jun 16, 2008


If to live in this style is to be eccentric, it must be confessed that there is something good in eccentricity.

Grimey Drawer
I'm try to make popup calendars to select a day+month+year or to select a month+year or to select a year, so I looked at the Picker class, which then directed me to the DatePickerDialog.

I want to make something like this:


But I can't seem to force it to show me only months or years.

I swear I've been able to do it before with other apps - or maybe it wasn't on Android?
Am I crazy? (Yes)

If I click part of the calendar that does popup, I can at least get it to show me some kind of year view. Maybe I should just go ahead and make my own version with a GridLayout.

Shadow0 fucked around with this message at 10:46 on Jan 16, 2019

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
Unfortunately, yes, you'll likely need to make it yourself. Try to use the Android styling values to have it match the device as much as possible here.

Lutha Mahtin
Oct 10, 2010

Your brokebrain sin is absolved...go and shitpost no more!

Shadow0 posted:

I'm try to make popup calendars to select a day+month+year or to select a month+year or to select a year, so I looked at the Picker class, which then directed me to the DatePickerDialog.

[...]

If I click part of the calendar that does popup, I can at least get it to show me some kind of year view. Maybe I should just go ahead and make my own version with a GridLayout.

are those the only date-picking classes in the whole android api? DatePickerDialog and DatePicker are from API level 1. i don't know much at all about this area of the API but there have definitely been some date/time pickers added over the years. for example, the clock-face time picker used in the AOSP Clock app was added (i think) sometime in the android 4.x-6.x range

Splinter
Jul 4, 2003
Cowabunga!

Lutha Mahtin posted:

there have definitely been some date/time pickers added over the years. for example, the clock-face time picker used in the AOSP Clock app was added (i think) sometime in the android 4.x-6.x range

There is also a TimePicker, but I believe what you're talking about have just been updates to the default DatePicker/TimePicker layouts/styles in newer API levels.

Lutha Mahtin
Oct 10, 2010

Your brokebrain sin is absolved...go and shitpost no more!

Does that mean it is possible to provide what OP is asking for simply through layout/style?

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
I'm pretty sure it goes beyond styling, the analog clock picker is fundamentally different from a spinner like it used to be and needs custom views for drawing / touching.

It's possible that there's month/year only functionality lurking somewhere but I wouldn't bet on it

FAT32 SHAMER
Aug 16, 2012



Does anyone have a recommendation for a charting/graphing lib? I've been using MPAndroidChart for my demo but it isnt very good in that the way you plot stuff is wonky and the amount of looping i need to do to force it to pull data from firebase is insanity.

I was looking at AnyChart but i get

code:
error: resource android:attr/dialogCornerRadius not found.
error: resource android:attr/dialogCornerRadius not found.
error: resource android:attr/fontVariationSettings not found.
error: resource android:attr/ttcIndex not found.
so I assume those are from old API levels or something

kitten smoothie
Dec 29, 2001

FAT32 SHAMER posted:

I was looking at AnyChart but i get
so I assume those are from old API levels or something

What's your compile/target SDK version? AnyChart is built against 28 so it shouldn't be referencing old stuff.

FAT32 SHAMER
Aug 16, 2012



kitten smoothie posted:

What's your compile/target SDK version? AnyChart is built against 28 so it shouldn't be referencing old stuff.

OH that might be the problem then, compileSdkVersion 27 buildToolsVersion '28.0.2'

gently caress

kitten smoothie
Dec 29, 2001

I wanna say there was a typo exactly like this in Android Studio’s starter template for build.gradle if I remember right.

FAT32 SHAMER
Aug 16, 2012



kitten smoothie posted:

I wanna say there was a typo exactly like this in Android Studio’s starter template for build.gradle if I remember right.

I upgraded my support stuff to api 28.0.0 and suddenly it couldnt find any of the views in my layouts and I couldnt use the constraint editor. Turns out its a Known Bug for API 28 and afaict they havent gotten around to fixing it yet.

I guess i may very well be stuck with MPAndroidChart :sigh:

Forgall
Oct 16, 2012

by Azathoth
I want to try to get into android development, and I have an idea for a small project to start with - an app that maintains websocket connection to remote server in background and pops a notification when it gets a message from the server. But from what I'm gathering there are some hard limits on what you are allowed to run in background, so is that possible? And in what direction should I be digging / what keywords should I google to figure out how to make it work?

Tunga
May 7, 2004

Grimey Drawer

Forgall posted:

I want to try to get into android development, and I have an idea for a small project to start with - an app that maintains websocket connection to remote server in background and pops a notification when it gets a message from the server. But from what I'm gathering there are some hard limits on what you are allowed to run in background, so is that possible? And in what direction should I be digging / what keywords should I google to figure out how to make it work?
Use Firebase Cloud Messaging to push the message from the server to the device. The device can then wake up and retrieve further data if it's needed.

https://firebase.google.com/docs/cloud-messaging/

Forgall
Oct 16, 2012

by Azathoth

Tunga posted:

Use Firebase Cloud Messaging to push the message from the server to the device. The device can then wake up and retrieve further data if it's needed.

https://firebase.google.com/docs/cloud-messaging/
Thanks! I'll look into that.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

Forgall posted:

I want to try to get into android development, and I have an idea for a small project to start with - an app that maintains websocket connection to remote server in background and pops a notification when it gets a message from the server. But from what I'm gathering there are some hard limits on what you are allowed to run in background, so is that possible? And in what direction should I be digging / what keywords should I google to figure out how to make it work?

For reference, you CAN do this, but you have to run as a foreground service so that the user can see that YOU'RE the one loving up their battery life.

But, push messaging is by far the right way to go here; the device will let you know when it's time to Do A Thing.

Forgall
Oct 16, 2012

by Azathoth

Volmarias posted:

For reference, you CAN do this, but you have to run as a foreground service so that the user can see that YOU'RE the one loving up their battery life.

But, push messaging is by far the right way to go here; the device will let you know when it's time to Do A Thing.
I wanted to play around with aws lambda's new websocket capabilities, but it looks like I'll have to use google's equivalent cloud functions instead.

Tunga
May 7, 2004

Grimey Drawer

Forgall posted:

I wanted to play around with aws lambda's new websocket capabilities, but it looks like I'll have to use google's equivalent cloud functions instead.
I'm not much of an expert on AWS but FCM has a web API that you can hit from literally anything that can make an HTTP request so you shouldn't be too locked in to any particular service:
https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream
This may make whatever specific new thing you wanted to play with redundant though.

CaptainJuan
Oct 15, 2008

Thick. Juicy. Tender.

Imagine cutting into a Barry White Song.
I'm just junior QA, not a Dev, but I feel like requesting the "draw over other apps" permission to display a password incorrect notification is....... Bad.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

CaptainJuan posted:

I'm just junior QA, not a Dev, but I feel like requesting the "draw over other apps" permission to display a password incorrect notification is....... Bad.

Yes it probably is.

:frogon:

Adbot
ADBOT LOVES YOU

CaptainJuan
Oct 15, 2008

Thick. Juicy. Tender.

Imagine cutting into a Barry White Song.
Other highlights include our entire scheduling system breaking down because our CTO's credit card was maxed out and our offshore devs outright refusing to follow the design scheme created by the recently-hired UX design team. I need a new jooooooob

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply