Saturday, 27 August 2016

AsyncTask in Android



Hello Friends,

Today I’ll discuss about “AsyncTask”. It stands for Asynchronous Task, which is one of the most important concept used in android development. It’s more or less related to thread concept used in various languages.

Definition : An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread.

Let’s understand this in this way, whenever we download anything using our browser, the downloading process is done by the browser in the background while we still surf the other web pages. It means, the downloading process doesn’t affect the working of browser. In this example we can relate the term UI with browser and downloading process with AsyncTask(background thread). It is highly recommended that we should not use AsyncTask for long operations.AsyncTask is an abstract class which is defined by 3 generic types, called Params, Progress and Result.  I personally recommend you to brush-up the concepts of Generic Types, Abstract Class and preferably File Handling before getting into this. 

In order to use AsyncTask class, we must subclass it.  

private class DownloadingTask extends AsyncTask<URL, Integer, Long> 
This AsyncTask class provides us the following methods to override and we must override at least one.  
1. OnPreExecute()
2. doInBackground(Params...)         [most often we override this method]
3. onProgressUpdate(Progress...)
4. onPostExecute(Result)
 
Note : Do not call any of the above mentioned methods manually. 

When an asynchronous task is executed, the task goes through 4 steps:
  1. onPreExecute(), As the method name suggests, it is invoked on the UI thread before the task is executed. In simple term we can understand this like, arranging all the required documents before going for an interview.
  2. doInBackground(Params...), This method is invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
     
  3. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field. 
     
  4. onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter. 
    source:
























Friday, 19 August 2016

Android Studio Shortcuts - II


We spend a lot of time navigating in our code base, let’s try to do it more efficiently.
Open class
cmd+o (Mac)
ctrl+n(Linux)
Imagine that you must go to a class named “MainActivity”, just use this shortcut and start typing “MainA”.
Open File
cmd+shift+o (Mac)
ctrl+shift+n(Linux)
Works like the “Open Class” shortcut but on every files in your project. It is very useful to open your AndroidManifest.xml or anything that sits in the res/ or assets/ folder.
Open Symbol
cmd+alt+o (Mac)
alt+shift+n(Linux)
A very powerful but little less known variation of the previous tips: You can use this to go directly to a method or field by searching its name!
For example, if you know that you have a method named getFormattedDate() somewhere in your project, you can type it in the Open Symbol dialog to go directly to it.
Partial Matching
You can enter incomplete strings and it will work. For example. if you are searching for a class named “ItemDetailFragment”, you can actually type “IDF” and it will find it.
Line Number
Imagine that your colleague just told you that the juicy part is in ExcitingClass at line 23. You can open the file directly by appending a “:” to the class name in the Open Class dialog. e.g.:
ExcitingClass:22
You can also combine it with partial matching and type something like:
EC:22
Recently opened files
cmd+e     (Mac)
ctrl+e   (Linux)
This will show a popup listing the files you navigated to previously.
Recently edited files
cmd+shift+e(Mac)
ctrl+shift+e(Linux)
Same as the above but listing only files that have been edited.

Navigate Back/Forward

cmd+alt+left/right (Mac)
ctrl+alt+left/right(Linux)
To understand this shortcut, think about how the back and forward buttons work in a web browser. Now, instead of thinking in web pages, think about source code! So when you drill down in code or open a new file, the IDE will remember where you where before, allowing you to quickly go back.

Last Edit Location

cmd+shift+backspace (Mac)
ctrl+shift+backspace(Linux)
This is a variation on the “Navigate Back” shortcut that cycles between the locations where you typed something.
Picture yourself fixing a nasty bug. You think you have the solution so you start fixing it but then realize that you have to look at the android source code and a couple other classes in your project. You enter a function, which leads you to another class, which leads you to another thing and 20 steps later, you finally have the insight needed to complete your fix… but in which file and at what line where you again? Just use this shortcut and you are right back at the exact line where you stopped writing.

 

Ref: http://www.developerphil.com/android-studio-tips-of-the-day-roundup-5/

Saturday, 13 August 2016

Android Studio Shortcuts


Are you a Android Developer ? You use android studio too? Well, if so is the case, I have something to share with you guys. I am gonna share some coolest keyboard shortcuts which you can use anytime.
Again like my last post, I am not sure whether these shortcuts will work on Windows or not. All I know is ....there is no harm in trying.
Let’s start now,


1. Highlight All the Things


cmd + Shift + F7 (Mac)
ctrl + Shift + F7 (Linux)


Select the text and press the above keys and it will highlight every occurrence of the selected text in the current file. We can then navigate up or down using the shortcuts from Edit → Find → Find Next/Previous
    We can cancel the highlighting by pressing Escape.
Additional tips:
  • Highlighting a “return” or a “throw” statement in a method will also highlight all the exit points of the method.
  • Highlighting the “extends” or the “implements” portion of the class definion will also highlight the methods that are overriden/implemented.
  • Highlighting an import will also highlight where it is used.


2. Move Between Methods and Inner Classes



ctrl + up/down (Mac)
alt + up/down (Linux)


This wil move our cursor to the name of the next method or class in the current file. If we are in the body of a method, going up will put the cursor on its name.


3. Move Lines Up/Down



alt+shift+up/down (for both Mac & Linux)


If we have to shift a line of code to some other place. Try this....!!


4. Delete Line



cmd + backspace (Mac)
ctrl + Y (Linux)

It deletes the current line or selection.

Reference : http://www.developerphil.com/android-studio-tips-of-the-day-roundup-1/

Saturday, 6 August 2016

Problem in starting android studio




Did you also get the following message while starting your android studio ?



  
Take a deep breath, As we are going to fix this issue. Well, since I faced and solved this issue using Windows Operating System. So I am not sure whether this solution will be applicable to other Operating Systems too. But you can always give a try .....you never know...It may work  !!!

okay ! so close your android studio and follow the steps given below.....

Step 1: You need to locate your Android Studio folder, then go to bin folder  and there you will find a file, named idea.properties.


Step 2: Add the following line of code into into the idea.properties file.

             disable.android.first.run = true

[What this will do is disable the check that Android Studio performs on first run and decides to download all that stuffs].


Step 3: Save this file at any location (with the same name) you wish. Then replace the original file (i.e., idea.properties) file with this updated one. 


Step 4: Restart the Android Studio and enjoy programming !!
(In case you are still facing this issue even after doing all the above mentioned steps. Try opening your Android Studio after rebooting your system)



References: 

https://www.youtube.com/watch?v=hXTzIbaQ9r8