Tag: coding

  • Prevent Huawei from Killing Your App //Android Dev Diary

    I have been playing around with Android app development, particularly on how to retain UI values after configuration changes and minimizing the app to launch a different one.

    To retain values after configuration changes, I use ViewModels.

    And to retain values after minimizing the app, the following is supposed to do the trick:

        override fun onSaveInstanceState(outState: Bundle) {
            super.onSaveInstanceState(outState)
             outState.putInt(KEY, number)
        }
        
        override fun onRestoreInstanceState(savedInstanceState: Bundle) {
            binding.textView.text = savedInstanceState.getInt(KEY)
        }
    

    But in the phone where I test most of my apps, it doesn’t! Minimizing my app to play a YouTube video, open a mobile game, and watch a few TikTok videos for about 15 minutes, causes my app’s UI data to reset.

    I installed the same app to another phone, a Samsung phone under the A Series. The user played a mobile game for several hours, and came back to my app to find all the previous UI values still intact!

    I remember that when I was still working with the Squadzip team (still grateful to Roman for suggesting that I try to practice “mindfulness” — it works wonders for a hectic lifestyle!), that Huawei phones gave us the same kind of issue. Apparently, Huawei phones are quite aggressive at killing apps in the background to preserve battery life.

    Options that I considered to prevent Huawei phone from killing my Android app in the background:

    • Save UI data to SharedPreferences, on top of using Bundle to save and restore instance state (hesitatant to do this because I don’t really need to save UI data)
    • Simply advise the user not to leave the app if they want their data to be retained (BAAAD IDEA!)
    • Find some sort of “whitelist” in Huawei settings to exempt my app from being aggressively killed

    The method I chose to prevent Huawei phone from killing my Android app:

    For the current app I’m working on, I chose to change a setting in the Huawei phone because it’s the easiest. It shouldn’t be so hard to explain to app users either.

    Instructions:

    • Go to Settings
    • Look for “Battery” and click on it
    • Look for “App launch” and click on it
    • Type the name of your app in the Search field.
    • Turn off the “Manage automatically” setting
    • Click OK on the prompt. (I chose to leave auto-launch, secondary launch, and run in background to keep my app alive.)
    • The description below your app’s name should now be “Manage manually” instead of “Manage automatically.”
  • DataBinderMapperImpl Error: cannot find symbol (DataBinding) //Android Kotlin Dev Diary

    Today I encountered the following error as I was building an Android app:

    DataBinderMapperImpl.java:18: error: cannot find symbol
    import net.catzie.myproject.databinding.ActivityMainBindingImpl;
    ^
    symbol: class ActivityMainBindingImpl
    location: package net.catzie.myproject.databinding

    Turns out, there’s an error in the related XML file, activity_main.xml! 😂

    For other possible solutions, this StackOverflower answer may help: android: data binding error: cannot find symbol class

  • Get “order” object/details from Thank You page of WooCommerce (WordPress)

    It’s been a long while since I last posted anything related to coding. I haven’t really stopped, but from Android app development, I switched back to PHP. Life has become occupied with other things, but when I do code, it’s in WordPress with WooCommerce. It’s for my online shop.

    In my opinion, WordPress is not the most pleasant platform to use if you’re a developer (after years of MVC coding for Android I find WordPress quite messy), but it’s what I already know. I don’t have time to learn another.

    Today’s code snippet is for customizing the Thank You page (post-checkout) by retrieving the “order” object.

    The $order_id is what we’ll start with. Then we get the order object with the help of the wc_get_order($order_id) method. Once you’re able to successfully call the order object, you may use its methods such as get_billing_first_name() to get the order details.

    You may refer to the WC_Order class page for a list of available methods.

    I originally got this code from Order Received (Thank you) page in WooCommerce and modified it to suit my needs. Check his blog out, as he has many helpful posts on how to customize WooCommerce!

  • Android app dev: Missing import org.apache.commons.lang3.text.WordUtils;


    I use a few instances of WordUtils.capitalizeFully() on an Android app that I’ve been working on. And there are times when the import org.apache.commons.lang3.text.WordUtils;
    breaks, sometimes after merging with some of my git branches.

    To fix this, what I do is open app/build.gradle and add the following line under dependencies:

    Hope this saves somebody else’s day 😆 I got the solution from How to add org.apache.commons.lang3 to AndroidStudio with gradle

  • Something to avoid when using Android RecyclerView

    When using Android’s RecyclerView, don’t use the same inflated XML layout file for items you intend to shuffle on the fly! Their contents will overlap.

    -CK

  • Setup/update Ruby on OS X El Capitan 10.11.5

    I need to learn Ruby on Rails for my new job. I’m a little thrilled about this chance to learn a new language, and I’m trying to get started with it on my Macbook which runs OS X El Capitan 10.11.5. I already have Ruby here, I don’t remember if pre-installed though. It’s out of date and I wanted to upgrade, as this resource Getting Started with Rails recommends version 2.2.2 or higher.

    First step I took was to get RVM (Ruby Version Manager):
    curl -sSL https://get.rvm.io | bash -s stable --ruby

    Then I received this message:

    ” * To start using RVM you need to run source /Users/catzie/.rvm/scripts/rvm
    in all your open shell windows, in rare cases you need to reopen all shell windows.”

    And so I ran:
    source /Users/catzie/.rvm/scripts/rvm

    Then:
    rvm install 2.4

    Which returned this message:

    Already installed ruby-2.4.0.
    To reinstall use:

    rvm reinstall ruby-2.4.0

    Finally, I ran this:
    rvm use 2.4 --default

    My next step was to install SQLite3 on my OS X as per instructions found here on SQLite – Installation.

    I got this message after following the instructions:

    SQLite version 3.17.0 2017-02-13 16:02:40
    Enter “.help” for usage hints.
    Connected to a transient in-memory database.
    Use “.open FILENAME” to reopen on a persistent database.
    sqlite>

    More on this in the future.

    Reference: Can I upgrade to the current version of Ruby (2.2.3) on OS X v10.6.8?

  • Add “options menu” to an AppCompatActivity (Android App Development)


    Adding an “options menu” to an Android app’s Activity is quite simple.

    This is an example of what an Android options menu looks like. Look at the icon that looks like 3 dots vertically stacked.

    android options menu

    My dev environment status as of writing this

    Android Studio version: v. 2.1
    OS: Mac OS X El Capitan v. 10.11
    My Android app build status:

    • compileSdkVersion: 24
    • buildToolsVersion: 24.0.2
    • minSdkVersion: 16
    • targetSdkVersion: 24

    Add “options menu” to an Android AppCompatActivity

    #1 Create menu layout file

    Create res/menu/main.xml, creating the menu folder if not yet existing.

    Add the following to res/menu/main.xml:

    #2 Enable menu by adding MenuInflater to the Activity

    Add the following overriding method to your AppCompatActivity:

    #3 Add listener for selected options menu item

    Again, add the following overriding method to your AppCompatActivity. Replace the Toasts with whatever you need to do upon menu item selection:

    That’s it! Your Android Activity should now have an options menu on the actionbar

    My resources for adding my options menu to Activity

    Adding the App Bar
    Android SDK: Implement an Options Menu