Pages

Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Sunday, August 2, 2015

Android activity life cycle and android studio debugging using logact.

Hi friends, today we are going to learn how to use logcat for debugging your code in android studio. For the demonstration I’ll use android activity life cycle J.If you are new to android there may be numerous questions in your mind about android activity lifecycle.
What is the lifecycle of android activity?
Why are those similar sounding methods (onCreate(), onStart(), onResume()) called during initialization,& others (onPause(), onStop(), onDestroy()) called at the end?




Normally android activity have 6 status
1.     Created
2.     Started
3.     Resumed
4.     Paused
5.     Stopped
6.     Destroyed

And android activity life cycle has 7 methods.Let’s get an idea about each of these methods from http://developer.android.com/

Method
Description
onCreate()
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.Always followed by onStart()

onRestart()
Called after your activity has been stopped, prior to it being started again.Always followed by onStart()

onStart()
Called when the activity is becoming visible to the user.Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

onResume()
Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
Always followed by onPause().
onPause ()
Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume(). When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.
onStop()
Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.

Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.
onDestroy()
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish () on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

 Ø When the Activity first time loads the events are called as below:
onCreate()àonStart()àonResume()

 Ø When you click on Phone button the Activity goes to the background and the below events are called:
onPause()àonStop()

 Ø Exit the phone dialer and the below events will be called:
onRestart()àonStart()àonResume()

 Ø When you click the back button OR try to finish() the activity the events are called as below:
onPause()àonStop()àonDestroy()

Now we are going to look at our hello world application which we gonna use for demonstrating above situations.Edit your MainActivity.java file as below.

MainActivity.java


You may wonder why I am using Log.d (“some text1”,”sometext2”) allover my code. The Android logging system provides a mechanism for collecting and viewing system debug output. Logcat dumps a log of system messages, which include things such as stack traces when the emulator throws an error and messages that you have written from your application by using the Log class.





I am using Log.d() to display my message.As you can see log class contains two parameters called tag & msg .Here I am using “Nadeeshaz” as the tag name for filtering purpose.for the msg I have used the relevant message for each section.



By pressing alt + 6 you will get the logcat window .It shows the logs for selected application. But there it’s really hard to find the log with our tag name.



Go to the drop down list(highlighted) in your right-hand side  & select “Edit filter configuration”.


When you click on it you will get the below window.


You have to fill Log Tag with you tag name in my case its “Nadeeshaz” and then you have to give a meaningful name for Filter Name .For more conventions I will use the same name for the filter name.




Then the logcat window will only show the messages with relevant tag value



Now I am going to open my HelloWorld app

When it open my logcat window shows below logs


When I press home  button

It gives below logs

Now I am in


When I open it again

It gives below logs

As the final step I click on the back button

These are the logs I got


I think this help you to understand a little about android activity life cycle and android studio debugging using logact J .

Tuesday, July 14, 2015

How to solve “Gradle DSL method not found:  ‘runProguard()'”in Android Studio Project 

Hello friends, today we are going to solve “Gradle DSL method not found: ‘runProguard ()'” Error in android studio.

You might get this error when you download android studio project from the internet and try to run it in your machine. This is because the application is developed using previous version of android studio & its app build.gradle file is not matching with your current version of android studio.

Let’s take a look at build.gradle file (app module)




Under buildTypes you will find
runProguard false

Which doesn’t exist anymore with later versions of android studio. So you have to replace this code with J

minifyEnabled false

Now your modified code segment should look like this



There are many useful projects in the internet which are publicly shared for learning purposes. Among those some of android studio projects may give this error because they are build using previous versions of android studio IDE. Edit your code and have a great experience :P 

Thursday, April 2, 2015

How to Setup Genymotion “A faster Android emulator” for Android Studio

Hey are you really worrying about your android emulator. So this is a real solution for that .Go to Genymotion website & create an account. https://www.genymotion.com/#!/

figure-01 


Then go to this link and download genymotion for windows (https://www.genymotion.com/#!/download )

figure-02


If you have not installed vitualbox in your machine get genymotion with virtualbox(117.47MB).As the first step install genymotion in your machine.

figure-03


Now open genymotion and click “+” sign on top of the window.Then sign in to your account for accessing all virtual devices in Genymotion.

figure-04


After creating your virtual device in genymotion what you need to do is adding genymotion plugin into android studio.To do that Open your android studio and go to settings.

figure-05


Type the word plugin in the search box of the setting window .Then click on “Brows Repositories”.Then search for “Genymotion”.

figure-06


figure-07


Then click on green color button call “install plugin” and it will request you to restart android studio.After that you will find a small button in your menu bar call “genymotion device manager”.For the first time when you click on this icon it will ask for the path for genymotion folder .In my case it was “C:\Program Files\Genymobile\Genymotion “.

After you finish that next time it will show you below window which contains all the virtual devices you have created.

figure-08


Select a virtual device you like and start it.After you run your project you will get choose device window and there you will find your genymotion virtual device under “choose a running device” option.
 
figure-09

figure-10






Almost done J !!!

Friday, March 27, 2015

How to solve “emulator: ERROR: x86 emulation currently requires hardware acceleration!” in Windows.

 When you try to run you program in android emulator you may get below error.
“Emulator: ERROR: x86 emulation currently requires hardware acceleration! Please ensure Intel HAXM is properly installed and usable. CPU acceleration status: HAX kernel module is not installed!”

To solve this error J

    1.    Check whether you have downloaded & installed the “intel x86 Emulator       Accelerator (HAXM installer)”. (At the bottom of the android SDK  Manager   you may find Extras).



   2.  Then check whether the “Hardware Virtualization” is “ENABLE” in your         BIOS.(I think this video will be helpful for you to enable virtualization in          your machine     https://www.youtube.com/watch?v=3irpIFya_lk )


   3.    Now go to your android SDK folder     Android\sdk\extras\intel\Hardware_Accelerated_Execution_Manager and   try to install” intelhaxm-android.exe”.


   4.    If you cannot install it go to this link, Click to Accept EULA & Download  haxm-windows_r05.zip (1.1.1) download and install it”  https://software.intel.com/en-us/android/articles/intel-hardware-accelerated-  execution-manager-end-user-license-agreement”.


   5.    If you still have a problem with installing intelhaxm.exe so then go to”  TURN WINDOWS FEATURE ON OFF “& disabled hyper-v then your  machine will ask to restart .After completing that try to install intelhaxm.exe  againJ.









  6.    Then try to start your AVD(Android Virtual Device) .Your Emulator should  run without giving any error