Saturday, August 1, 2020

Memory leak in startactivityforresult

If we have to start Activity B in Activity A for result as follow 

We will call startActivityForResult from Activity A we will set some result in out activity B and return result.
But what if user presses back button on Activity B how should we handle this case.

I was running LeakCanary and found surprising result that it was always leaking my Activity B. reason is that it wasn't finishing and staying in memory even after returning to Activity B with Back Press.

So here is the solution that I found to resolve this. So quick fix is call method finishActivity with request code that you have passed while calling activity B. 

class ActivityA: AppCompatActivity(){

    private val requestCode = 1001

    fun startActivityB(){
    startActivityForResult(Intent(this, ActivityB::class.java).apply 
                {
                    putExtra("category_id", currentCategory._id)
                    putExtra("category_color", currentCategory.color)
                    putExtra("category_name", currentCategory.name)

                }, requestCode)
    }

    override fun onActivityReenter(resultCode: Int, data: Intent?) {
        super.onActivityReenter(resultCode, data)
        finishActivity(requestCode)
    }

}

Saturday, June 6, 2020

RxAndroid basic example

RxAndroid is flavour of reactive programming implementation for Android.

Base of reactive programming is Observer design pattern.

Let's understand Observer design pattern first.

Observer Design Pattern:
Observer design pattern is one of the behavioural design pattern. It's about communication between objects. In observer design pattern main object emits the change in it and all the object s who are observing to change in main object gets notification about it.


Friday, December 13, 2019

using Java, Kotlin and dagger together in project

If we are using java, kotlin and dagger together in project.

Follow this steps

kapt
Kapt is the Kotlin Annotation Processing Tool, and it’s in pretty good shape these days. If you want to be able to reference generated code from Kotlin, you need to use kapt. To do that, simply include the plugin in your build.gradle file with the line:


apply plugin: 'kotlin-kapt'







And in dependencies

kapt 'com.google.dagger:dagger-compiler:2.13'

Thursday, June 27, 2019

Block touch event in view

Add an onTouchEvent method to the view with top position then return true. True will tell the event bubbling that the event was consumed therefore prevent event from bubbling to other views.

protected boolean onTouchEvent (MotionEvent me) {
    return true;
}

For v1 you would do an import:
import android.view.View.OnTouchListener;
Then set the onTouchListener:

v1.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});

Force hide soft keyboard

InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    //Find the currently focused view, so we can grab the correct window token from it.
    View view = activity.getCurrentFocus();
    //If no view currently has focus, create a new one, just so we can grab a window token from it
    if (view == null) {
        view = new View(activity);
    }
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

difference between “px”, “dip”, “dp” and “sp”

  1. px
    Pixels - corresponds to actual pixels on the screen.
  2. in
    Inches - based on the physical size of the screen.
    1 Inch = 2.54 centimeters
  3. mm
    Millimeters - based on the physical size of the screen.
  4. pt
    Points - 1/72 of an inch based on the physical size of the screen.
  5. dp or dip
    Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
  6. sp
    Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

Tuesday, June 18, 2019

Running adb over wifi

Easy Steps for running adb over wifi

1.  Connect Android phone and host machine to same WiFi network

2.  Connect Android phone to host machine using USB cable (to start with)

3.   Run adb tcpip 5555 from a command prompt

4.   Run adb shell "ip addr show wlan0 | grep -e wlan0$ | cut -d\" \" -f 6 | cut -d/ -f 1" to obtain the phone's IP address

5.   Disconnect USB cable and run adb connect :5555