Understanding Android Activity Lifecycle (Part Four)

1. when the user close the application forcefully
2. when Android it self destroy the Activity.
3. when user hits the back button

Unfortunately you can not distinguish between 1 and 2.

But you can do something when user clicks the back button since onPause() is called after onBackPressed(),
so you can put a flag in the onBackPressed(). That way you can distinguish how it landed here

onBackPressed() -> onPause() or

home button / multitask button -> onPause()

Enjoy 🙂

Advertisements

Final, Finally (Part 3: Final Method)

welcome to part 3 of Final Keyword …

Final Method

We can declare a method as final. A final method cannot be overridden by a child class.

public class Dhaka {
    public final void uCanNotOverrideMe (){
        System.out.println("wow!");
    }   
}

public class Khulna extends Dhaka {
     public final void uCanNotOverrideMe (){
        System.out.println("wow!");  
     }
}

// Compile error: uCanNotOverrideMe() in Khulna cannot override uCanNotOverrideMe() in Dhaka; overridden method is final

If you don’t want a child class to override a method for some of the requirements, then you can use final methods.

enjoy!

Final, Finally (Part 2: Final Class)

welcome to part 2 of Final Keyword. Lets Start …

Final Class

We can declare a class as final. After that we cant create child class under that final class.

example time.


public final class King {
    public static void main (String args []){
        System.out.println("king Arthur");   
    }
}

public class Test extends King {
    
}

// compile error: cannot inherit from final King

so why on earth you want a final class,

1. Final classes (Immutable) are read-only.
2. Thread safe, no synchronization overhead.

a very good explanation

Enjoy 🙂

Final, Finally (Part 1: Final Variables)

Well The Java keyword final has nothing to do with this tv series 🙂

Final Variable

A final variable cannot be changed after it has been assigned a value.

Java convention says that you should write you final constants in uppercase and use underscore (_) to separate multiple words. Here is a basic example

public class Test {
    public static void main (String args []) {
        final int SECOND = 60;
        final String WATCH_NAME = "SNOW";
        System.out.println(SECOND);
        System.out.println(WATCH_NAME);
    }
}

Let’s messing around

        final int SECOND = 60;
        final String WATCH_NAME = "SNOW";
        System.out.println(SECOND);
        System.out.println(WATCH_NAME);
        SECOND = 60; // error

Compile time error: cannot assign a value to final variable SECOND

hm seems nice

public class Test {
        final int SECOND = 60;
        final String WATCH_NAME = "SNOW";
}

public class IwantToChangeYou {
    public static void main (String args []) {
        Test t = new Test();
        t.SECOND = -60; // error
    }
}

Compile time error in IwantToChangeYou class: cannot assign a value to final variable SECOND in class.

public class Test {
    final int SECOND = 60;
    final String WATCH_NAME = "SNOW";
    Test(int val){
        this.SECOND = val; // error
    }
}

public class IwantToChangeYou {
    public static void main (String args []) {
        Test t = new Test(60);
    }
} 

Compile time error in Test class: cannot assign a value to final variable SECOND.


/* A blank final is a final variable whose declaration lacks an initializer. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared */

public class IwantToChangeYou {
    
    final int SECOND; 
    
    IwantToChangeYou(){
        this.SECOND = 60;
    }
   
    public static void main (String args []) {
        System.out.println("yes we can set the final value at run time!");
    }
    
}
// but if we use static final we cant do this 

public class IwantToChangeYou {
    
    static final int SECOND; // A blank final is a final variable whose declaration lacks an initializer. 
    
    IwantToChangeYou(){
        this.SECOND = 60;
    }
   
    public static void main (String args []) {
        System.out.println("yes we can set the final value at run time!");
    }
    
}

// option a
static final int SECOND  = 10; 

//option b
public class IwantToChangeYou {
    
    static final int SECOND;
    
    static {
        SECOND = 10;
    }
   
    public static void main (String args []) {
        System.out.println("yes we can set the final value at run time!");
    }
    
}

// we can also use local final variables

public class IwantToChangeYou {
    
    public static void main (String args []) {
        System.out.println("whhat");
    }
    
    public int whaaat(final int NO_WAY){
        int lol = NO_WAY;
        return lol;
    }
}


// Compile time error when we tried to change it
 public int whaaat(final int NO_WAY){
        NO_WAY = 99; // error
        int lol = NO_WAY;
        return lol;
}

public class IwantToChangeYou {
    static final int NO_WAY;
    static int OK;
    IwantToChangeYou(int a){
        this.OK = a;   
        System.out.println("id 1");
    }
    static {
        System.out.println("id 2");
        NO_WAY = OK;
    }  
}

public class Test {
    public static void main (String args []) {
        IwantToChangeYou hm = new IwantToChangeYou(77);
        System.out.println("set final static variable "+hm.NO_WAY);
    } 
}

// expected output ??
// well its 0, because static block executed first when a class initialized
/*
id 2
id 1
set final static variable 0
*/

References
link
link

Enjoy!

Java and….and Sublime Text (MAC OS X) Part 2

First lets see some screen shots

Screen Shot 2013-05-15 at 10.02.45 PM

Screen Shot 2013-05-15 at 10.29.30 PM

Hmm now i am going tell how to open Sublime Text from Terminal, just follow this page

you may ask for permission issues when you enter the code into terminal, just simply add sudo at the beginning. And if it says the path not found then you have to create the bin folder write in the terminal, mkdir bin

If you have not succeeded, then you have to do some work …

1. Open terminal
2. vim .bash_profile
3. type i, to get access to write
4. you will see this line, export PATH
5. add this, $PATH:~/bin
6. Finally you should have something like this, export PATH=$PATH:~/bin
7. press esc then press shift + q
8. write, wq

restart terminal, now try command subl in the terminal

you can pass many types of argument just check the link i gave earlier in this post.

Thanks to Shouro Vaia 😀

Enjoy…

Java and….and Sublime Text (MAC OS X) Part 1

Hello everyone, are you looking for a good text editor then i guess you might try Sumlime.

Here is the link for download.
Select OS X.

After opening Sublime you will see the following window.

Screen Shot 2013-05-15 at 8.31.54 PM

After that you can drag and drop your project folder

Screen Shot 2013-05-15 at 9.06.19 PM

In the picture you can see i opened a java code. If you want to highlight syntax like mine follow the steps below

Screen Shot 2013-05-15 at 9.10.51 PM

go to view -> Syntax -> Java -> java
🙂

Well now i am going to talk about code formatting.

For this you have to install wbond

Installation is through the Sublime Text 2 console. This is accessed via the ctrl+` shortcut. Once open, paste the following command into the console.

import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print('Please restart Sublime Text to finish installation')

After the installation press, command + shift + P then type “Install” and select “Package Control: Install Package”. After that Select “SublimeAStyleFormatter” from list.

more

If everything goes write press

Ctrl+Alt+F: Format current file.
⌘+K, ⌘+F: Format current selection.

Enjoy …

Count the unique character in a String

public static int frq [] = new int [500];    
public static int countUniqueChar (String line) {
      Arrays.fill(frq, 0);
      int ans = 0;
      for(int i = 0; i < line.length(); ++i) {
          if(frq[line.charAt(i)] == 0) {
              frq[line.charAt(i)]++;
              ++ans;
          }
      }
      return ans;
}  

input
“aaaaaa”
“aaa aaa”
“abcdeabcde”
“YESyes”

output
1
2
5
6