activity传递数据1

activity如何传递数据:

package com.example.androidtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent it = getIntent();
        System.out.println(it);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

打印出来的结果:

03-03 14:07:15.721: I/System.out(930): 
Intent { 
act=android.intent.action.MAIN 
cat=[android.intent.category.LAUNCHER] 
flg=0x10000000 
cmp=com.example.androidtest/.MainActivity 
}

这是不是就是安卓清单文件定义的:




    

    
        
            
                

                
            
        
    


我们继续添加第二个Activity,看看两个活动如何跳转.

此时的activity_main.xml文件:


    
    

清单文件:



    
    
        
            
                

                
            
        
        
            
        
    

从MainActivity.class启动:

package com.example.androidtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(
                new OnClickListener(){
                    @Override
                    public void onClick(View arg0) {
                        Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                        //方式1:使用putExtra方法:
                        intent.putExtra("name","ssss");
                        intent.putExtra("age", 17);
                        startActivity(intent);
                       //方式2:使用putExtras方法:
                       /*
                       Bundle myBundelForName=new Bundle(); 
                       myBundelForName.putString("name","ssss"); 
                       myBundelForName.putString("age",17); 
                       intent.putExtras(myBundelForName); 
                      */
                    }
                }
        );
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

SecondActivity.java

package com.example.androidtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class SecondActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        Intent it = getIntent();
        //方式1
        Log.i("secondactivity",it.getStringExtra("name"));
        //方式2
        Bundle bundle = it.getExtras();
        bundle.get("name");
        bundle.get("age");
       //另外,既然bundle对象类似map,可以实现遍历
    }
}

此时在SecondActivity中 System.out.println(it); 输出的内容是:

03-04 02:14:28.867: I/System.out(567): Intent { cmp=com.example.androidtest/.SecondActivity }

安卓文档上,对该方法的解释是:

Activity (Test API)
public Intent getIntent()
Return the intent that started this activity.

较好的传递数据的方法:

    public static void actionStart(Context context,String data1,String data2){
           Intent intent = new Intent(context,SecondActivity.class);
           intent.putExtra("params1", data1);
           intent.putExtra("params2", data2);
           context.startActivity(intent);
    }

这样,从任意的Activity启动SecondActivity都变得容易,要传递的数据也一目了然。如下:

SecondActivity.actionStart(FirstActivity.this,data1,data2);

你可能感兴趣的:(activity传递数据1)