package com.example.study01;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivityImplicit extends AppCompatActivity {
EditText et_msg;
TextView tv_bottom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_implicit);
et_msg = findViewById(R.id.et_msg);
tv_bottom = findViewById(R.id.tv_bottom);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data!=null){
String username = data.getStringExtra("username");
String password = data.getStringExtra("password");
String sessionId = data.getStringExtra("sessionId");
tv_bottom.setText("username: " + username +" password: " + password +" sessionId: " + sessionId );
}
}
public void Search(View view) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(),MainActivityLogin.class);
intent.putExtra("msg",et_msg.getText().toString());
startActivityForResult(intent,1);
//startActivity(intent);
}
}
package com.example.study01;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Instrumentation;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivityLogin extends AppCompatActivity {
TextView tv_msg;
EditText username;
EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_login);
// et_msg = findViewById(R.id.et_msg);
tv_msg = findViewById(R.id.tv_Login1);
Intent intent = getIntent();
tv_msg.setText("搜索内容:" + intent.getStringExtra("msg"));
username = findViewById(R.id.username);
password = findViewById(R.id.password);
}
public void Login(View view) {
Intent intent = new Intent();
intent.putExtra("username",username.getText().toString());
intent.putExtra("password",password.getText().toString());
intent.putExtra("sessionId","sad1sd4245dsf42as");
setResult(1,intent);
finish();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivityImplicit">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_msg"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="搜索"
android:textSize="30sp"
android:onClick="Search"/>
<TextView
android:id="@+id/tv_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="等待参数回传"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivityLogin"
android:orientation="vertical">
<TextView
android:id="@+id/tv_Login1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="17sp"/>
<TextView
android:id="@+id/tv_Login2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户未登录...."/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:onClick="Login">
</Button>
</LinearLayout>
Homework summary:
The difference between startActivity( ) and startActivityForResult( )
1. The difference between startActivity( ) and startActivityForResult( )
1. startActivity( )
startActivity( ) just starts another Activity. It will not automatically return to the original Activity. If If you want to jump back to the original page, you must use startActivity() again to start the original Activity.
2. startActivityForResult()
can complete this task in one go. When the program executes this code, if it jumps from FirstActivity to SecondActivity, after the SecondActivity executes the finish() method, the program will automatically call back FirstActivity’s onActivityResult ( int requestCode, int resultCode, Intent intent) methods.
So override this method to call the intent and display the parameters.
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data!=null){
String username = data.getStringExtra("username");
String password = data.getStringExtra("password");
String sessionId = data.getStringExtra("sessionId");
tv_bottom.setText("username: " + username +" password: " + password +" sessionId: " + sessionId );
}
}