Thursday, April 18, 2013

Passing an object as parameter to a method

Passing an object as parameter is always a confusing concept. here is a java code given below which might help you understand this concept .

/*========================================*/

public class HelloWorld{
     public static void main(String []args){
       myroom obj = new myroom();
       dadsroom obj1 = new dadsroom();
       obj1.love(obj);
     }
}
 class myroom{
public int x;
    public int y =3;
}
class dadsroom{
 void love(myroom o){
       System.out.println(o.x +"and"+ o.y);
   }
}
/*========================================*/

SHOULD I CALL IT EXPLANATION ? :
 I've created three class in above code, one is HelloWorld which combines the main method, next is myroom, and last one is dadsroom. In main method, I've created an object of myroom "obj" and another object of dadsroom "obj1". dadsroom is having a method love which is receiving object of myroom and then printing instance variable using object of myroom. In main, I've called love method using object of class dadsroom and passing object of myroom  as parameter..

Let's have a formal EXPLANATION:
what i am trying to do is, i am having a template of myroom and also a template of dadsroom through their objects, now I am adding a room which is same as myroom(every thing is same as myroom cause it is instance of myroom) into dadsroom, now a room which is same as myroom is attached with dadsroom, I can access every feature (which was having myroom) through dadsroom. An object of myroom is attached with dadsroom by passing object of myroom.

Sunday, April 14, 2013

More about Activity Life Cycle with an example.

Hey there, Here is an example of Activity life cycle, This will illustrate more about how onCreate(), onStart(), onResume, onPause(), onStop() call in activity Life Cycle, I have Created two activity and given toast in each method, when you navigate the application Toast will pop on the screen as per Activity Life cycle.

<!-- ================First layout (main.xml)=====================-->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

<!-- ===================End of main.xml====================== -->


/*=========ActivityLifeCycleActivity (Corrosponding code to main.xml) ========*/
package com.hellboys.act;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ActivityLifeCycleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toast.makeText(getBaseContext(), "1 onCreate()",Toast.LENGTH_LONG).show();
       
        Button b1;
        b1 = (Button) findViewById(R.id.but);
        b1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(ActivityLifeCycleActivity.this,abhi.class);
startActivity(intent);
}
});
    }
   
    public void onStart(){
    super.onStart();
    Toast.makeText(getBaseContext(), "1 onStart()",Toast.LENGTH_LONG).show();
    }
   
    public void onResume(){
    super.onResume();
    Toast.makeText(getBaseContext(), "1 onResume()",Toast.LENGTH_LONG).show();
    }
   
    public void onPause(){
    super.onPause();
    Toast.makeText(getBaseContext(), "1 onPause()",Toast.LENGTH_LONG).show();
    }
   
    public void onStop(){
    super.onStop();
    Toast.makeText(getBaseContext(), "1 onStop()",Toast.LENGTH_LONG).show();
    }
}
/*=============End of ActivityLifeCycleActivity=================*/

<!-- ================Second layout (abhi.xml)==================-->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
<!-- =====================End of abhi.xml======================= -->


/*=========Second Activity(Java code corresponding to abhi.xml) ========= */
package com.hellboys.act;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class abhi extends Activity{
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.abhi);
Toast.makeText(getBaseContext(), "2 onCreate()", Toast.LENGTH_LONG).show();
}

public void onStart(){
super.onStart();
Toast.makeText(getBaseContext(), "2 start()", Toast.LENGTH_LONG).show();
}
public void onResume(){
super.onResume();
Toast.makeText(getBaseContext(), "2 onResume()", Toast.LENGTH_LONG).show();
}
public void onPause(){
super.onPause();
Toast.makeText(getBaseContext(), "2 onPause()", Toast.LENGTH_LONG).show();
}
public void onStop(){
super.onStop();
Toast.makeText(getBaseContext(), "2 onstop()", Toast.LENGTH_LONG).show();
}
}
/*=================End of abhi============================*/


/*==========================Do +1 if like ....... ========================*/