質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

687閲覧

2つのサブクラス間でintentを使用してデータのやり取りをしたい

numatuka

総合スコア9

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

1クリップ

投稿2019/12/01 11:47

#前提・実現したいこと
Mainactivityからではなく、SubclassAからintentでSubclassBに飛び、SubclassAに値を返したいです。

該当のソースコード

activitymain

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <TextView 10 android:id="@+id/textView" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="Hello World!" 14 app:layout_constraintBottom_toBottomOf="parent" 15 app:layout_constraintLeft_toLeftOf="parent" 16 app:layout_constraintRight_toRightOf="parent" 17 app:layout_constraintTop_toTopOf="parent" /> 18 19 <Button 20 android:id="@+id/button" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:layout_marginStart="8dp" 24 android:layout_marginTop="8dp" 25 android:layout_marginEnd="8dp" 26 android:layout_marginBottom="8dp" 27 android:text="Button" 28 app:layout_constraintBottom_toBottomOf="parent" 29 app:layout_constraintEnd_toEndOf="parent" 30 app:layout_constraintHorizontal_bias="0.517" 31 app:layout_constraintStart_toStartOf="parent" 32 app:layout_constraintTop_toBottomOf="@+id/textView" 33 app:layout_constraintVertical_bias="0.174" /> 34 35 <Button 36 android:id="@+id/button3" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:layout_marginStart="8dp" 40 android:layout_marginTop="8dp" 41 android:layout_marginEnd="8dp" 42 android:layout_marginBottom="8dp" 43 android:text="Button" 44 app:layout_constraintBottom_toBottomOf="parent" 45 app:layout_constraintEnd_toEndOf="parent" 46 app:layout_constraintHorizontal_bias="0.517" 47 app:layout_constraintStart_toStartOf="parent" 48 app:layout_constraintTop_toBottomOf="@+id/button" 49 app:layout_constraintVertical_bias="0.124" /> 50 51</android.support.constraint.ConstraintLayout>

MainActivity

1package com.example.intenttest; 2 3import android.support.v7.app.AppCompatActivity; 4import android.os.Bundle; 5import android.view.View; 6import android.widget.Button; 7import android.widget.TextView; 8 9public class MainActivity extends AppCompatActivity { 10 private Sub sub; 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 16 Button go = findViewById(R.id.button); 17 Button ret = findViewById(R.id.button3); 18 sub =new Sub(MainActivity.this); 19 go.setOnClickListener(new View.OnClickListener() { 20 @Override 21 public void onClick(View view) { 22 sub.goSubb(MainActivity.this); 23 } 24 }); 25 ret.setOnClickListener(new View.OnClickListener() { 26 @Override 27 public void onClick(View view) { 28 sub.ret(MainActivity.this); 29 } 30 }); 31 32 } 33}

Sub

1package com.example.intenttest; 2 3import android.content.Intent; 4import android.widget.TextView; 5 6import static android.app.Activity.RESULT_OK; 7 8class Sub { 9 public static final String EXTRA_MESSAGE = "テスト"; 10 static final int RESULT_SUBACTIVITY = 1000; 11 String res=""; 12 Sub(MainActivity mainActivity){ 13 TextView kekka = (TextView) mainActivity.findViewById(R.id.textView); 14 } 15 16 public void goSubb(MainActivity mainActivity){ 17 Intent intent = new Intent(mainActivity.getApplication(), Subb.class); 18 TextView kekka = (TextView) mainActivity.findViewById(R.id.textView); 19 if(kekka.getText() != null){ 20 String str = "なにもない"; 21 intent.putExtra(EXTRA_MESSAGE, str); 22 } 23 mainActivity.startActivityForResult(intent, RESULT_SUBACTIVITY); 24 } 25 protected void onActivityResult( int requestCode, int resultCode, Intent intent) { 26 27 if(resultCode == RESULT_OK && requestCode == RESULT_SUBACTIVITY && 28 null != intent) { 29 res = intent.getStringExtra(Sub.EXTRA_MESSAGE); 30 } 31 } 32 public void ret(MainActivity mainActivity){ 33 TextView kekka = (TextView) mainActivity.findViewById(R.id.textView); 34 kekka.setText("かえり"+res); 35 } 36}

subb

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.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 9 <TextView 10 android:id="@+id/textView2" 11 android:layout_width="wrap_content" 12 android:layout_height="13dp" 13 android:layout_marginStart="8dp" 14 android:layout_marginEnd="8dp" 15 android:layout_marginBottom="8dp" 16 android:text="TextView" 17 app:layout_constraintBottom_toBottomOf="parent" 18 app:layout_constraintEnd_toEndOf="parent" 19 app:layout_constraintStart_toStartOf="parent" 20 app:layout_constraintTop_toTopOf="parent" /> 21 22 <Button 23 android:id="@+id/button2" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_marginStart="8dp" 27 android:layout_marginEnd="8dp" 28 android:layout_marginBottom="8dp" 29 android:text="Button" 30 app:layout_constraintBottom_toBottomOf="parent" 31 app:layout_constraintEnd_toEndOf="parent" 32 app:layout_constraintHorizontal_bias="0.498" 33 app:layout_constraintStart_toStartOf="parent" 34 app:layout_constraintTop_toBottomOf="@+id/textView2" 35 app:layout_constraintVertical_bias="0.13" /> 36</android.support.constraint.ConstraintLayout>

Subb

1package com.example.intenttest; 2 3import android.content.Intent; 4import android.os.Bundle; 5import android.view.View; 6import android.widget.Button; 7import android.widget.TextView; 8 9public class Subb extends MainActivity{ 10 private String message; 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.subb); 15 Intent intent = getIntent(); 16 message = intent.getStringExtra(Sub.EXTRA_MESSAGE); 17 18 final TextView textView = findViewById(R.id.textView2); 19 textView.setText(message); 20 21 // back to MainActivity 22 Button button = findViewById(R.id.button2); 23 button.setOnClickListener(new View.OnClickListener() { 24 public void onClick(View v) { 25 Intent intent = new Intent(); 26 if (textView.getText() != null) { 27 String str = "返ってきた"; 28 intent.putExtra(Sub.EXTRA_MESSAGE,str); 29 } 30 31 setResult(RESULT_OK, intent); 32 33 finish(); 34 } 35 }); 36 } 37}

#発生している問題・エラーメッセージ
画面遷移でMainとSub画面を行き来することは出来ていますが、"返ってきた"の文字列をMain画面に表示することが出来ません。Intentの処理をどこか間違えていると思うので、そこに気が付いたなら教えてくれるとありがたいです。

#自分で調べたこと
Intentの処理に関して様々なページを見ましたが、MainActivityからの遷移が多くSubClass同士での遷移は見つかりませんでした。

#環境
Windows10
AndroidStudio 3.4.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2019/12/01 14:25

> ```MainActivity ```の右にはファイル名ではなく言語名(java, xml 等)をご記入ください. ファイル名は, 枠外か, 枠内にその言語のコメント形式でご記入ください.
jimbe

2019/12/01 14:28

> class Sub { > protected void onActivityResult( int requestCode, int resultCode, Intent intent) { この onActivityResult は呼ばれているのでしょうか.
numatuka

2019/12/01 20:01

回答だけを確認していて気が付きませんでした、申し訳有りません。 アドバイスありがとうございます。次回からは気をつけさせていただきます。
guest

回答1

0

ベストアンサー

startActivityForResultやstartActivityで呼び出す時しか、「intent.putExtra」で渡すことは出来ないと思います。

以下、intになってますがStringも扱えます。
Preferencesを使えば、別のActivity、Serviceで書いたものを読み込んだり書き込んだり出来るので便利です。

Java

1 SharedPreferences sharedPreferences; 2 SharedPreferences.Editor editor; 3 4 sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 5 editor = sharedPreferences.edit(); 6 7 //フラグメントID設定 8 int fragmentId = 1; 9 editor.putInt("fragmentId", fragmentId); 10 editor.commit(); 11 12 //フラグメントID読込 13 fragmentId = sharedPreferences.getInt("fragmentId", 0);

投稿2019/12/01 12:51

編集2019/12/01 13:07
jun74

総合スコア338

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

numatuka

2019/12/01 15:36

intentでデータを受け渡せるかどうかばかりを考えていましたが、別の関数で受け渡せるということは知りませんでした。 これで悩んでいたところが解決しました、ありがとうございました。
jimbe

2019/12/02 02:53

出来るか出来ないかでは「出来る」方法ですが, SharedPreferences は本来極一時的な値をやり取りするために使うものではないことはご留意ください. もし SubclassB がアプリの設定画面等で, 永続させる値を返しているのでしたら, こちらが最善かと思います. (その場合「受け渡し」とは少し違う状態ですが.)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問