エラーメッセージの通り、 Intent に putExtra(String, User) というメソッドはありません。
任意のオブジェクトを渡す場合は putExtra(String, Parcelable) か putExtra(String, Serializable) を利用することになります。
例えば User クラスに Serializable を implements しては如何でしょうか。
MainActivity.java
java
1import androidx.appcompat.app.AppCompatActivity;
2
3import android.content.Intent;
4import android.os.Bundle;
5import android.widget.Button;
6
7import java.io.Serializable;
8
9public class MainActivity extends AppCompatActivity {
10 public static final String EXTRA_USER = "user";
11
12 @Override
13 protected void onCreate(Bundle savedInstanceState) {
14 super.onCreate(savedInstanceState);
15 setContentView(R.layout.activity_main);
16
17 Button button = findViewById(R.id.button);
18 button.setOnClickListener(v -> {
19 User user = new User("id1", "name2");
20 Intent intent = new Intent(this, SubActivity.class);
21 intent.putExtra(EXTRA_USER, user);
22 startActivity(intent);
23 });
24 }
25}
26
27class User implements Serializable {
28 final String id, name;
29 User(String id, String name) {
30 this.id = id;
31 this.name = name;
32 }
33 @Override
34 public String toString() {
35 return getClass().getSimpleName()+"[id="+id+", name="+name+"]";
36 }
37}
res/layout/activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<androidx.constraintlayout.widget.ConstraintLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:app="http://schemas.android.com/apk/res-auto"
5 xmlns:tools="http://schemas.android.com/tools"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 tools:context=".MainActivity">
9
10 <Button
11 android:id="@+id/button"
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content"
14 android:text="Hello World!"
15 app:layout_constraintBottom_toBottomOf="parent"
16 app:layout_constraintEnd_toEndOf="parent"
17 app:layout_constraintStart_toStartOf="parent"
18 app:layout_constraintTop_toTopOf="parent" />
19
20</androidx.constraintlayout.widget.ConstraintLayout>
SubActivity.java
java
1import androidx.appcompat.app.AppCompatActivity;
2
3import android.content.Intent;
4import android.os.Bundle;
5import android.widget.TextView;
6
7public class SubActivity extends AppCompatActivity {
8 @Override
9 protected void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.activity_sub);
12
13 Intent intent = getIntent();
14 User user = (User)intent.getSerializableExtra(MainActivity.EXTRA_USER);
15
16 TextView textView = findViewById(R.id.text);
17 textView.setText("" + user);
18 }
19}
res/layout/activity_sub.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<androidx.constraintlayout.widget.ConstraintLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:app="http://schemas.android.com/apk/res-auto"
5 xmlns:tools="http://schemas.android.com/tools"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 android:background="#f0f000"
9 tools:context=".SubActivity">
10
11 <TextView
12 android:id="@+id/text"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:text="Sub World!"
16 app:layout_constraintBottom_toBottomOf="parent"
17 app:layout_constraintEnd_toEndOf="parent"
18 app:layout_constraintStart_toStartOf="parent"
19 app:layout_constraintTop_toTopOf="parent" />
20
21</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml: Application タグ内アクティビティ定義
xml
1 <activity
2 android:name=".MainActivity"
3 android:exported="true">
4 <intent-filter>
5 <action android:name="android.intent.action.MAIN" />
6
7 <category android:name="android.intent.category.LAUNCHER" />
8 </intent-filter>
9 </activity>
10 <activity
11 android:name=".SubActivity"
12 android:exported="false"
13 android:parentActivityName=".MainActivity">
14 <meta-data
15 android:name="android.support.PARENT_ACTIVITY"
16 android:value=".MainActivity" />
17 </activity>