Android learning (using Intent to pass data)

We already know that Intent can start activity explicitly or implicitly. In fact, there are many uses of Intent, such as starting services, sending broadcast messages, etc. Let's introduce how to use Intent to pass data.

  • Intent syntaxIntent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(uriString))
  • The first parameter is the action to be performed by the intent, and the second parameter is the data to be passed. This data can be an integer or character, or an address, class, and so on.
    Insert picture description hereWe combine this demo to introduce how Intent transfers data.

The first is to transfer the name of the first page to the second page, we use the intent.putExtra method. Below is the code for the click event.

listView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String name = list.get( position );
        Intent intent = new Intent( test_main.this,test_add.class );//第二个参数是要传递给的类的名字
        intent.putExtra( "name",name );
        intent.addFlags( position );	//添加一个当前listview位置的标记
        startActivityForResult( intent,1 );  //先不看
    }
} );

When we order the takeout, the takeout will not fly over directly, but will be delivered by the takeout brother, who first loads the takeout into the car and then delivers it to our home. The role of the intent is similar to the takeaway boy, temporarily storing the data, and then sending the data to the place where it should be sent.

Now we successfully put the value of name in the Intent, the next step is to accept the data, and then return a value to tell whether the first page is deleted. This is the key code of the second page.

TextView textView = findViewById( R.id.textview );
final Intent intent = getIntent();
final String name = intent.getStringExtra( "name" );//将Intent的数据保存到一个变量中
final int position = intent.getFlags();  //获取位置标记
textView.setText( name );

Button confirm = findViewById( R.id.confirm );
confirm.setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        intent.addFlags( position );//设置返回位置标记
        setResult( RESULT_OK,intent );返回intent
        finish();
    }
} );

Note here that you must call the setResult() method when you return. The first is the result code, and the second is the return value. The result code RESULT_OK represents normal return data. But we just return on the second page, it's not enough, we need to receive it on the first page.

@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
    switch (requestCode){
        case 1:
            if(resultCode == RESULT_OK){
                int positionFlag =  data.getFlags();
                list.remove( positionFlag ); //删除listview中的值
                adapter.notifyDataSetChanged();//更新listview列表
            }
            break;
        default:
    }
}

We rewrite the onActivityResult method on the first page. Here case: 1, 1 represents the request code, which is the second parameter of the first code without looking at the position. In this way, we have mastered the simple Intent method of transferring data.

Conclusion

  • In fact, intent delivery information is more used for broadcast messages (Brodercast), combined with Intent filters, can deliver messages to other applications. Interested students can continue to study.

Guess you like

Origin blog.csdn.net/qq_42893430/article/details/89977547