package com.geek.Test01;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class SplashActivity extends Activity {
public static final String TITLE = "title";
public static final String USER_INFO = "userInfo";
public static final int REQUEST_CODE = 9999;
private static final String TAG = SplashActivity.class.getSimpleName();
Handler mHandler = new Handler();
private TextView mTextView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mTextView= (TextView) findViewById(R.id.title_text_view);
final String title = mTextView.getText().toString();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
//跳转到MainActivity
UserInfo userInfo = new UserInfo("小明",12);
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
intent.putExtra(TITLE,title);
intent.putExtra(USER_INFO, userInfo);
startActivityForResult(intent, REQUEST_CODE);
}
},1000);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG,"requestCode:"+ requestCode + ",resultCode:"+resultCode);
if (requestCode == REQUEST_CODE && resultCode == MainActivity.RESULT_CODE){
if (data != null){
String title = data.getStringExtra(TITLE);
mTextView.setText(title);
}
}
}
}
package com.geek.Test01;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CODE = 9999;
public static final int RESULT_CODE = 1234;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//当被点击时触发的行为
Intent intent = new Intent();
intent.putExtra(SplashActivity.TITLE,"我是主页,我送礼回来了");
setResult(RESULT_CODE,intent);
//关闭
finish();
//会跳回去 Intent intent = new Intent(MainActivity.this,SplashActivity.class);
// startActivity(intent);
}
});
Intent intent = getIntent();
if (intent != null){
String title = intent.getStringExtra(SplashActivity.TITLE);
UserInfo userInfo = (UserInfo) intent.getSerializableExtra(SplashActivity.USER_INFO);
setTitle("名字是" + userInfo.getmUserName());
}
}
}
package com.geek.Test01;
import java.io.Serializable;
import java.lang.ref.SoftReference;
public class UserInfo implements Serializable {
private String mUserName;
private int mAge;
public UserInfo(String userName , int age){
mUserName = userName;
mAge = age;
}
public String getmUserName(){
return mUserName;
}
public void setmUserName(String userName){
mUserName = userName;
}
public int getmAge(){
return mAge;
}
}
<?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=".MainActivity"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/splashBackgroundColor"
android:text="@string/splash_text"/>
<Button
android:id="@+id/button_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/button_margin"
android:text="第一个页面"/>
<Button
android:id="@+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/button_margin"
android:text="第二个页面"/>
<Button
android:id="@+id/button_third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/button_margin"
android:text="第三个页面"/>
<Button
android:id="@+id/button_fourth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/button_margin"
android:text="第四个页面"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/splashBackgroundColor"
>
<TextView
android:id="@+id/title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="@color/white"
android:text="@string/splash_text"/>
</LinearLayout>