activity之间跳转

2017-06-08  本文已影响0人  yanghanbin_it

传递数据

   /**
    * 隐式跳转至第二个activity
    * 
    * @param v
    */
   public void click4(View v) {
       // 方式一可行 同在一个intent-filter下 
       Intent intent = new Intent();
       intent.setAction("com.example.activeswitch.second");
       intent.setDataAndType(Uri.parse("hello:123"), "text/username");
       startActivity(intent);
   }  
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Intent intent = getIntent();
        Uri uri = intent.getData(); // hello:123
        String type = intent.getType(); //  text/username
    }  

// 第一个Activity
public void click7(View v) {
        Intent intent = new Intent();
        intent.setAction("com.example.activeswitch.second");
        // 方式一
        intent.putExtra("username", "hello world");
        intent.putExtra("password", "android");
        // 方式二
        Bundle bundle = new Bundle();
        bundle.putString("username", "hello android");
        bundle.putString("password", "123");
        intent.putExtras(bundle);
        startActivity(intent);
    }  
//第二个Activity
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Intent intent = getIntent();
        
        //方式一
        /*String username = intent.getStringExtra("username");
        String password = intent.getStringExtra("password");*/
        
        
        //方式二
        Bundle bundle = intent.getExtras();
        String username = bundle.getString("username");
        String password = bundle.getString("password");
        
        System.out.println("username:" + username + " password:" + password);
    }  

应用场景

上一篇 下一篇

猜你喜欢

热点阅读