Activity之间的交互-创新互联

在第一个activity中使用

创新互联公司是一家专业提供岚山企业网站建设,专注与网站设计制作、成都网站制作、H5技术、小程序制作等业务。10年已为岚山众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
startActivityForResult(intent, state_tag);

启动第二个activity,在第二个activity中调用 setResult(,)

在第一个activity中重载onActivityResult()

第一个Activity:

public class MainActivity extends Activity {
    private TextView tv = null;
    private int state_tag = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.textView);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
                                
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("content", tv.getText().toString());
                i.putExtra("bundle", bundle);
                startActivityForResult(i, state_tag);
            }
        });
    }
    @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
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == state_tag)
        {
            if(resultCode == RESULT_OK)
            {
                                    
                Log.i("MainActivity", data.getBundleExtra("bundle2").getString("newContend"));
                tv.setText(data.getBundleExtra("bundle2").getString("newContend"));
            }
            else
            {
                Log.i("MainActivity", "in else:");
                return;
            }
        }
    }
}

第二个activity:

package com.example.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SecondActivity extends Activity {
    private EditText editText = null;
    private Button okButton = null;
    private Button cancelButton = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
          
        editText = (EditText)findViewById(R.id.editText);
        okButton = (Button)findViewById(R.id.okButton);
        cancelButton = (Button)findViewById(R.id.cancelButton);
        Intent i = getIntent();
        editText.setText(i.getBundleExtra("bundle").getString("content"));
        okButton.setOnClickListener(new View.OnClickListener() {
              
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(SecondActivity.this, MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("newContent", String.valueOf(editText.getText()));
                i.putExtra("bundle2", bundle);
                setResult(RESULT_OK, i);
                finish();
            }
        });
        cancelButton.setOnClickListener(new View.OnClickListener() {
              
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                setResult(RESULT_CANCELED);
                finish();
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
    }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章标题:Activity之间的交互-创新互联
URL网址:http://ybzwz.com/article/ccgdco.html