Tuesday, 1 March 2011

This is Blog about Android Intent how to work with android Intent. in this Example i take one example in which we pass two value one Activity to another Activity through Intent. to Be Ready.

Take One New Project
you must take 2 layout , and 2 java class file.

type Bellow Code to Your Project
package com.drc.sharedpre;

/* First Java File Code */
import android.app.Activity;
import android.content.Intent;  
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.ContactsContract.Intents;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;  

public class sharedprencedActivity extends Activity {
      /** Called when the activity is first created. */
           Button sv;
           EditText roll,nm;
          @Override

           public void onCreate(Bundle savedInstanceState) {    
                         super.onCreate(savedInstanceState);
                          setContentView(R.layout.main);  
                    sv=(Button)findViewById(R.id.btnSaveact1);  
                    roll=(EditText)findViewById(R.id.etxtRollno);
    nm=(EditText)findViewById(R.id.etxtName); 
sv.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
  // TODO Auto-generated method stub Toast.makeText(sharedprencedActivity.this,"This is all",Toast.LENGTH_SHORT);
 if(v==sv) {
  Intent in = new Intent(sharedprencedActivity.this,secondAct.class);
  SharedPreferences spf= getSharedPreferences("flHitesh",MODE_WORLD_WRITEABLE); SharedPreferences.Editor spe= spf.edit(); spe.putString("speRollNo",roll.getText().toString());
spe.putString("speNm", nm.getText().toString()); spe.commit(); startActivity(in); 
     }
    }
  }); 
}
@Override  
protected void onRestart(){
       super.onRestart(); 
       SharedPreferences  
                 spf=getSharedPreferences("flHitesh",MODE_WORLD_READABLE); 
                  String srollno,sName; srollno=spf.getString("speRollNo","jay jay");  
                  sName=spf.getString("speNm", "No Value"); roll.setText(srollno);
                   nm.setText(sName); 
   }
}


/* second Java File Code */
package com.drc.sharedpre;
import android.app.Activity;
import android.content.Intent;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.style.SuperscriptSpan; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class secondAct extends Activity { 
          public String rl,n; 
          public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.viewdata); 
                TextView tvRollno,tvNm; 
                
               final Button btnEdit; 
              tvRollno=(TextView)findViewById(R.id.txtRollAct2);  
              tvNm=(TextView)findViewById(R.id.txtnameAct2); 
              btnEdit=(Button)findViewById(R.id.btnEditAct2); 
             
           SharedPreferences sp= getSharedPreferences("flHitesh",MODE_WORLD_READABLE); 
         String rl=sp.getString("speRollNo", "11");
         String n=sp.getString("speNm", "Jay Shree");
         tvRollno.setText(rl);
         tvNm.setText(n); 
     btnEdit.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
  // TODO Auto-generated method stub
          if(v==btnEdit) {
                     Intent in = new Intent(secondAct.this,thirdAct.class);
                     startActivity(in);
            }
         } 
     });
 }
}


/* Third Java File Code */
package com.drc.sharedpre; 
import android.app.Activity;  
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button;
import android.widget.EditText; 

public class thirdAct extends Activity{ 
         public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState); 
              setContentView(R.layout.editdata); 
              secondAct sa=new secondAct(); 
             final EditText rl; 
             final EditText nm; 
             final Button sv;
          rl=(EditText)findViewById(R.id.etxt_rollno_act3);
          nm=(EditText)findViewById(R.id.etxt_nm_act3); 
          sv=(Button)findViewById(R.id.btn_save_act3);  
          sv.setOnClickListener(new OnClickListener() {
               
         public void onClick(View v) {
                    // TODO Auto-generated method stub 
                   if(v==sv) { 
                 SharedPreferences 
                 sp=getSharedPreferences("flHitesh",MODE_WORLD_WRITEABLE);   
                 SharedPreferences.Editor spe=sp.edit();
                 spe.putString("speRollNo",rl.getText().toString());
                 spe.putString("speNm", nm.getText().toString()); spe.commit(); 
            } 
        }
  }); 
           
  SharedPreferences sp=  
                      getSharedPreferences("flHitesh",MODE_WORLD_READABLE);  
                      SharedPreferences.Editor sped=sp.edit(); 
                      String rll=sp.getString("speRollNo", "11"); 
         String n=sp.getString("speNm", "Jay Shree"); 
         sped.putString("speRollNo", rll); 
         sped.putString("speNm", n);
         rl.setText(rll);
         nm.setText(n); 
      }
 }