Archive | February, 2012

Android training : activity life cycle phases, when to use which function.

Activity life cycle : different phases 

onCreate()        – to —  onDestroy()  ==> ENTIRE LIFE TIME of ACTIVITY 

onStart()            – to —  onStop() ==> VISIBLE LIFE TIME of ACTIVITY

onResume()      – to  –  onPause() ==> FOREGROUND/ ACTIVELY RUNNING LIFE TIME of ACTIVITY

 

When should I use different functions of Activity life cycle ?

onCreate

  1. The system calls this when creating your activity.
  2.  you should initialize the essential components of your activity. Eg: setcontentview
  3. Using findviewbyid(), to programmatically interact with widgets in the UI,
  4. calling managedQuery
  5. You can call finish()
  6. If you want any thread running in the background to download DATA, you can do it here. This has to be deleted in onDestroy()
  7. Derived classes must call through to the super class’s implementation of this method. If they do not, an exception will be thrown.

onStart

  1. Called after onCreate(Bundle)  or after onRestart() followed by onResume().
  2. you can register a BroadcastReceiver in onStart() to monitor changes that impact your UI, You hav to unregister it in onStop()
  3. Derived classes must call through to the super class’s implementation of this method. If they do not, an exception will be thrown.

onResume

  1. Called after onRestoreInstanceState(Bundle)onRestart(), or onPause()
  2.  begin animations, open exclusive-access devices (such as the camera)

onPause ()

  1. Called as part of the activity lifecycle when an activity is going into the background
  2. The counterpart to onResume().
  3. B will not be created until A’s onPause() returns. (So don’t do anything long here)
  4. Save all your persistent data here.
  5. Good place to do things like stop animations (any CPU consuming things), to make switching to other activity as soon as possible.
  6. Close your camera here if it is opened
  7. Eg: if device goes to sleep or some dialog is displayed, then it will go to onPause().
  8. onStop() will follow this function, once the other waiting activity resumes and started displaying on the screen. This is not guaranteed as in some cases onResume() will be called with out onStop().
  9. Derived classes must call through to the super class’s implementation of this method. If they do not, an exception will be thrown.

onStop ()

  1. Called when you are no longer visible to the user
  2. You will next receive either onRestart()onDestroy()
  3.  this method may never be called, in low memory situations
  4. Derived classes must call through to the super class’s implementation of this method. If they do not, an exception will be thrown.

onDestroy ()

 

  1. Perform any final cleanup before an activity is destroyed
  2. do not count on this method being called as a place for saving data!
  3. This method is usually implemented to free resources like threads that are associated with an activity,
  4. Derived classes must call through to the super class’s implementation of this method. If they do not, an exception will be thrown

onRestart ()

  1. Called after onStop() when the current activity is being re-displayed to the user
  2. It will be followed by onStart() and then onResume()
  3. If you have deactivated any cursor in onStop(), call managedQuery() again.
  4. Derived classes must call through to the super class’s implementation of this method. If they do not, an exception will be thrown.

Posted in ANDROID0 Comments

Android training: Starting one activity from other activity

Note: Before starting this program, it is expected that programmer should already create two activities with below names in androidmanifest.xml file.

Summary: This program will depict how to start one activity from other activity

 

File name: SubActivityActivity.java

 

package com.satishandroid.subactivity;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

//import

 

public class SubactivityActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button b= (Button) findViewById(R.id.button1);

b.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

startActivity(new Intent(getApplicationContext(), SecondActivity.class));

}

});

//b.

}

}

——————————————————————————————————————

File name: SecondActivity.java

 

package com.satishandroid.subactivity;

 

import android.app.Activity;

import android.os.Bundle;

 

public class SecondActivity extends Activity {

 

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.secondactivity);

// TODO Auto-generated method stub

}

 

}

————————————————————————————————————

File name: Main.xml

 

<?xml version=“1.0″ encoding=“utf-8″?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:orientation=“vertical”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

>

<TextView

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:text=“@string/hello”

/>

<Button android:text=“Button” android:id=“@+id/button1″ android:layout_width=“wrap_content” android:layout_height=“wrap_content”></Button>

<TextView android:text=“TextView” android:textAppearance=“?android:attr/textAppearanceSmall” android:layout_height=“wrap_content” android:layout_width=“wrap_content” android:id=“@+id/textView1″></TextView>

</LinearLayout>

 ——————————————————————————————————————

File name: Secondactivity.xml

 

<?xml version=“1.0″ encoding=“utf-8″?>

<LinearLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

android:orientation=“vertical”

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<TextView android:text=“Second activity is started”

android:id=“@+id/textView1″

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”></TextView>

</LinearLayout>

 

Posted in ANDROID0 Comments

Android training: Button clicking example

//File name: ButtonclickActivity.java

// Note: before executing this program, you should ensure that you have one button & one edittext in your main.xml file.

package com.techpalle.buttonclick; // This is the package name in which your project will reside.

/*
* Below are the import statements which will import different classes which you will be
* using in your program through out.
*/

import android.app.Activity; // This will import package needed to use Acitivity class in your code
import android.os.Bundle;// This will import Bundle class for savedInstanceState
import android.view.View;// This is needed if you are using View class in your code
import android.view.View.OnClickListener;// This will be needed if you are creating instance for OnClickListener interface
import android.widget.Button;//for using Button related things
import android.widget.EditText;// needed when you are using EditText instances.

/*
* ButtonclickActivity is our Activity name (which is a class actually)
* And we are extending Activity class.
* Activity –> is a single, focused thing that is used to interact with User
* Activity class takes care of creating a window for you in which you can place your UI
*/

public class ButtonclickActivity extends Activity {
/** Called when the activity is first created. */
/*
* onCreate is the first method to get called by Android, before launching your activity screen.
*/

@Override
public void onCreate(Bundle savedInstanceState) {

/*
* Since you are using Activity base class, which actually gives you the basic
* fundamental functionalities for your activity. So, you have to call super.onCreate
* before you proceed with your stuff.
*/

super.onCreate(savedInstanceState);
setContentView(R.layout.main);// This sets your main.xml stuff to your activity screen.

/*
* Now our problem is to put some text in editText1 screen if some one clicks on
* button1. So here are the steps to be followed to do it.
* 1. get resource ref of editText1
* 2. get resource ref of button1
* 3. set onClickListener for button11,so that android will call that function if some one
* clicks on that button.
* 4. Now last thing, once android calls that onclick functionality, do what ever you want
* in our case we will fill some text in editText1
*/

final EditText t = (EditText) findViewById(R.id.editText1);
Button b = (Button) findViewById(R.id.button1);
/* setOnClickListener will take object to OnClickListener interface, so we need to implement that interface and give that ref to it. */
b.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
t.setText(“Button has been clicked”);
// TODO Auto-generated method stub
}
});
}
}

Posted in ANDROID0 Comments

Dotnet supported platforms

Dotnet is supported in the below platforms.

  • Windows 98 or later versions
  • Linux and Mac OS ( Linux and Mac are supported by using Dotnet MONO Project)

Note: Sign Up For .NET Training

Posted in .NET Framework FAQ0 Comments

Custom Exceptions in C#

In C# all Custom Exception should be inherited from a class called as ApplicationException.

In the below sample i created a Custom Exception class called CustSalException. I am raising an Exception when some creates an object of Salary class by passing salary value less than 5000.

Please see the below code for the sample. In the code we are also logging the Error message when ever an exception is encoutered into the error log file.

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

int a = 3000;

try

{

Salary ss = new Salary(a);

}

catch (CustSalException cse)

{

File.WriteAllText(@”C:\error.txt”, “Error Message:”+cse.msg);

}

}

}

public class Salary

{

private int _salary;

public Salary(int sal)

{

if (_salary &lt; 5000)

{

CustSalException ee = new CustSalException();

ee.msg = “Salary Should be more than 5000rs”;

throw ee;

}

else

_salary = sal;

}

}

public class CustSalException : ApplicationException

{

public string msg;

}

Posted in .NET Framework FAQ0 Comments

Ternary Operators in C#

Ternary operators can be use full when we have only one simple if else condition.

Ternary Operator Syntax:

 

condition ? first_expression : second_expression;
Ex:
public class TernaryEx { public int GetBigNum(int a, int b) { return a &gt; b ? a : b; //in the above syntax if a is greater than b //the above expression returns a else it returns b. } }
Note: SignUp for JobGuaranteed Training

Posted in C# FAQ0 Comments

is and as operators in C#

is Operator:

  • is operator Checks if an object is compatible with a given type
  • An is expression evaluates to true if the provided expression is non-null, and the provided object     can  be cast to the provided type without causing  an exception to be thrown.

as Operator:

  • The as operator is used to perform conversions between compatible reference types
  • The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception

Ex:


public class Exam

{

    public string GetSubjectName()

    {

        return “C#”;

    }

}

public class UnitTest

{

    public int GetTestNumber()

    {

        return 1;

    }

}

public partial class CSHARPSAMPLES_Keywords : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        Exam e1 = new Exam();

    }

    public void Test(object o)

    {

        if (o is UnitTest)

        {

            UnitTest u = o as UnitTest;

            u.GetTestNumber();

        }

        else if (o is Exam)

        {

            Exam e = o as Exam;

            e.GetSubjectName();

        }

    }

}

Posted in C# FAQ0 Comments

Difference between == and Object.Equals Method in C#

protected void Page_Load(object sender, EventArgs e)

{

object s1 = “abc”;

object s2 = “abc”;

if (s1 == s2)

{

Response.Write(“value of s1 and s2 are equal”);

}

else if(s1.Equals(s2))

{

Response.Write(“Address of s1 and s2 are equal”);

}

/***************************************************************

* 1.==compares values of the variables.

* 2.Object.Eqals Method compares address of the variables

* **************************************************************/    }

Author: Raghavendra

Click here for job guarantee courses in bangalore

Posted in C# FAQ0 Comments

Step By Step Procedure to Consume a WebService

1.Create a webservice and click browse on its .asmx to see the url of the asmx file 2.Open Asp.net application and Right click on the Project Select AddWebreference and give the above generated asmx url , click on Go.

3.Change the Default WebReference name to “Palle” Click on Add Reference.

                                                    (OR)

Instead of using Above steps You can Achieve same thing using WSDL.exe

Cosuming WebSerice Using WSDL.EXE:

  1. After creating the Webservice Right click on .asmx file and Click on Browse
  2. Copy the url
  3. Open VS2008 Command prompt
  4. Follow the steps given in the below given screen shot.

Posted in WebServices0 Comments