做过好几个简单的安卓小项目后,常常觉得自己的水平常常处于能做出来东西,但是原理等有时会很模糊,所以重温重温原理


首先是四大组件之 Activity:

首先是Activity的主要框架:

public class  Activity extends

@Override
public void
super.onCreate(savedInstanceState);    
  setContentView(R.layout.lifecycle);  
}

@Override
protected void
{
super.onStart();  
}

@Override
protected void
super.onRestart();  
}

@Override
protected void
super.onResume();  
}  

@Override
protected void
super.onPause();  
}

@Override
protected void
super.onStop();  
}
@Override
protected void
super.onDestroy();  
}

//对比常见的这几个方法还有三种用的多的方法

//Activity窗口获得或失去焦点时被调用,在onResume之后或onPause之后
@Override
    public void onWindowFocusChanged(boolean hasFocus) {
       super.onWindowFocusChanged(hasFocus);
        Log.i(TAG, "onWindowFocusChanged called.");
    }

/**

 


 
 


     * Activity被系统杀死时被调用.

     * 例如:屏幕方向改变时,Activity被销毁再重建;当前Activity处于后台,系统资源紧张将其杀死.
     * 另外,当跳转到其他Activity或者按Home键回到主屏时该方法也会被调用,系统是为了保存当前View组件的状态.
     * 在onPause之前被调用.
     */
@Override
protected void
"param", param);  
"onSaveInstanceState called. put param: "
super.onSaveInstanceState(outState);  
    }  
      
/**
     * Activity被系统杀死后再重建时被调用.
     * 例如:屏幕方向改变时,Activity被销毁再重建;当前Activity处于后台,系统资源紧张将其杀死,用户又启动该Activity.
     * 这两种情况下onRestoreInstanceState都会被调用,在onStart之后.
     */
@Override
protected void
"param");  
"onRestoreInstanceState called. get param: "
super.onRestoreInstanceState(savedInstanceState);  
    }  
}

数据传送方面:一个activity跳到另外一个activity,附带传值有好几种方式:传的值都是简单的,若要传ArrayList,则要使用Serializable或Parcalable
最常用的:
Intent it=new Intent(A.this,TwoMainActivity.class);      //A到B,  附带值1       
    it.putExtra("key", 1);
    startActivity(it);B接收:
Bundle bundle=new Bundle();
   bundle=this.getIntent().getExtras();
   String sw=bundle.getString("key");                 //根据A发来的值类型进行接收
传ArrayList:
首先来个数据类:
public class MyClass implements Serializable{
  private String username;
  private String psw;
  private  int age;
  public String getUsername() {
   return username;
  }
  public void setUsername(String username) {
   this.username = username;
  }
  public String getPsw() {
   return psw;
  }
  public void setPsw(String psw) {
   this.psw = psw;
  }
  public int getAge() {
   return age;
  }
  public void setAge(int age) {
   this.age = age;
  }}

然后从A到B:
 ArrayList<MyClass> arrayList=new ArrayList<MyClass>();
    for(int i=0;i<10;i++)
    {
      MyClass myclass=new MyClass();
      myclass.setUsername("a"+i);
      myclass.setPsw("b"+i);
      myclass.setAge(1+i);
      arrayList.add(myclass);
    }
    Intent its=new Intent(this,TwoMainActivity.class);
    its.putExtra("key", arrayList);
    startActivity(its);
B接收:
 ArrayList<MyClass> arrayList=(ArrayList<MyClass>) this.getIntent().getSerializableExtra("key");
   String result="";
   for(MyClass myclass:arrayList)
   {
    result+=myclass.getUsername()+"----"+myclass.getPsw()+"---"+myclass.getAge()+"\n";
   }
使用Parcelable方法来传送:为什么有两种,Parcelable虽然复杂,但是对内存有益,所以用:
来个数据类:
public class MyClass2 implements Parcelable{
  
  private String username;
  private String psw;
  private  int age;
  public String getUsername() {
   return username;
  }
  public void setUsername(String username) {
   this.username = username;
  }
  public String getPsw() {
   return psw;
  }
  public void setPsw(String psw) {
   this.psw = psw;
  }
  public int getAge() {
   return age;
  }
  public void setAge(int age) {
   this.age = age;
  } //静态Parcelable.Creator接口
  public static final Parcelable.Creator<MyClass2> CREATOR=new Creator<MyClass2>()
    {
   
    public MyClass2 createFromParcel(Parcel source)
    {
     MyClass2 myClass2=new MyClass2();
     myClass2.setUsername(source.readString());
     myClass2.setPsw(source.readString());
     myClass2.setAge(source.readInt());
     return myClass2;
    }
    public MyClass2[] newArray(int size)
    {
     return new MyClass2[size];
    }
    };
  @Override
  public int describeContents() {
   // TODO Auto-generated method stub
   return 0;
  } //将数据写入外部提供的Parcel
  @Override
  public void writeToParcel(Parcel arg0, int arg1) {
   arg0.writeString(this.getUsername());
   arg0.writeString(this.getPsw());
   arg0.writeInt(this.getAge());
   
  }
  
  
  }

传送:
ArrayList<MyClass2> arrayList=new ArrayList<MyClass2>();
    for(int i=0;i<10;i++)
    {
      MyClass2 myclass=new MyClass2();
      myclass.setUsername("a"+i);
      myclass.setPsw("b"+i);
      myclass.setAge(1+i);
      arrayList.add(myclass);
    }
    Intent its=new Intent(this,TwoMainActivity.class);
   its.putExtra("key", arrayList);   
    startActivity(its);
    
接收:
   ArrayList<MyClass2> arrayList=(ArrayList<MyClass2>)this.getIntent().getSerializableExtra("key");
   String result="";
   for(MyClass2 myclass:arrayList)
   {
    result+=myclass.getUsername()+"----"+myclass.getPsw()+"---"+myclass.getAge()+"\n";
   }
注意:无论是SerialzableExtra还是Parcel    :
传送都用putExtra("key","value");
接收都是this.getIntent().getSerializableExtra("key");



接下来介绍另外一中启动:A跳B,传一个值给B,B销毁时,返回给A一个值:
Intent its=new Intent(A.this,TwoMainActivity.class);     A到B,并且传一个请求码:1
   
    startActivityForResult(its, 1);
在B中:
 Intent its=new Intent(this,MainActivity.class);
   its.putExtra("s", "swf");
   setResult(100, its);                                //返回码穿回给A,A可以通过返回码取得Intent中的值
在A中,接收销毁时返回来的码
@Override
  protected void onActivityResult(int requestCode,int resultCode,Intent data)
  {
   int request=requestCode;
   int ssw=resultCode;
   Bundle bundle=data.getExtras();
   String asd="";
   if(bundle!=null)
   {
    asd=bundle.getString("s");
   }
   
   String ss=asd;
   String sw=ss;
  }

现总结至此: