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

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

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

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

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

Android Studio

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

Q&A

解決済

1回答

2403閲覧

Android Studio)Fragmentをshow()/hide()で切り替える

akai_kinomi

総合スコア18

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

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

Android Studio

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

0グッド

0クリップ

投稿2019/03/28 04:56

編集2019/03/29 02:51

実現したいこと

Android Studioを使ってAndroidアプリを作成しています。
このプログラムで、下記動作を実現したいです。
・frag1でボタンを押す(textView:frag1_World)
・frag2に切り替える
・再度frag1に切り替えたとき、frag1_Worldが表示された状態にする。(画面保持)

MainActivity.javaimport

1import android.os.Bundle; 2import android.support.annotation.RequiresApi; 3import android.support.v4.app.FragmentManager; 4import android.support.v4.app.FragmentTabHost; 5import android.support.v7.app.AppCompatActivity; 6import android.util.Log; 7import android.widget.TabHost.TabSpec; 8 9 10public class MainActivity extends AppCompatActivity { 11 12 private FragmentTabHost mTabHost; 13 private String[] TabTag = { "Dice", "Character"}; 14 15 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 if (savedInstanceState == null) { 22 23 //FragmentManagerの取得 24 FragmentManager mFragmentManager = getSupportFragmentManager(); 25 26 // FragmentTabHostを取得する 27 mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 28 29 // Context、FragmentManager、タブ選択時にFragmentを挿入するリソースID 30 mTabHost.setup(this, getSupportFragmentManager(), R.id.content); 31 32 //String型の引数には任意のidを渡す 33 //今回は2つのFragmentをFragmentTabHostから切り替えるため、2つのTabSpecを用意する 34 TabSpec mTabSpec1 = mTabHost.newTabSpec("tab1"); 35 TabSpec mTabSpec2 = mTabHost.newTabSpec("tab2"); 36 37 //Tab上に表示する文字を渡す 38 mTabSpec1.setIndicator("frag1"); 39 mTabSpec2.setIndicator("frag2"); 40 41 Bundle args = new Bundle(); 42 args.putString("string", "message"); 43 44 45 mTabHost.addTab(mTabSpec1, Fragment1.class, args); 46 mTabHost.addTab(mTabSpec2, Fragment2.class, args); 47 Log.d("debug","add"); 48 49 } 50 } 51} 52

activity_main.xml<!

1<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@android:id/tabhost" 3 xmlns:ads="http://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="@color/background" 7 > 8 <!-- --> 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:orientation="vertical" > 13 <!-- タブパネル操作のためのTabWidgetを配置 --> 14 <TabWidget 15 android:id="@android:id/tabs" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:layout_weight="0" 19 android:orientation="horizontal" /> 20 <!-- タブパネルを束ねるためのコンテナーを配置 --> 21 <FrameLayout 22 android:id="@android:id/tabcontent" 23 android:layout_width="0dp" 24 android:layout_height="0dp" 25 android:layout_weight="0" /> 26 <!-- タブが押された時に表示するコンテンツ領域(Fragment) --> 27 <FrameLayout 28 android:id="@+id/content" 29 android:layout_width="match_parent" 30 android:layout_height="0dp" 31 android:layout_weight="1" 32 android:layout_marginTop="20dp" 33 android:layout_marginBottom="20dp" 34 android:layout_marginLeft="10dp" 35 android:layout_marginRight="10dp" 36 /> 37 </LinearLayout> 38</android.support.v4.app.FragmentTabHost>

Fragment1.java

1import android.support.annotation.Nullable; 2import android.support.v4.app.Fragment; 3import android.os.Bundle; 4import android.util.Log; 5import android.view.LayoutInflater; 6 7 8public class Fragment1 extends Fragment { 9 10 TextView textView1; 11 Button button1; 12 13 public void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 16 // 初期設定 -------------------------------------------------------------------------------- 17 //addTabの際にBundleを渡す場合は、Bundleから値を取得する 18 //渡さない(nullを渡している)場合は、実装しなくてよい。そうでないとgetString("string")時にエラーが発生する 19 Bundle args = getArguments(); 20 String str = args.getString("string"); 21 //------------------------------------------------------------------------------- 22 } 23 24 @Nullable 25 @Override 26 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 27 final View v = inflater.inflate(R.layout.frag1, null); 28 29 30 Log.d("debug", "frag1_OnCreateView"); 31 32 // text ------------------------------------------------------------------------------------ 33 textView1 = v.findViewById(R.id.text_view1); 34 textView1.setText("Frag1_Hello"); 35 36 button1 = v.findViewById(R.id.button1); 37 button1.setOnClickListener(new View.OnClickListener() { 38 @Override 39 public void onClick(View v) { 40 textView1.setText("frag1_World"); 41 } 42 }); 43 44 45 return v; 46 } 47 48 @Override 49 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 50 super.onViewCreated(view, savedInstanceState); 51 Log.d("test", "createFragment1"); 52 } 53 54 @Override 55 public void onSaveInstanceState(Bundle outState) { 56 String i = "Hi"; 57 outState.putString("editText", i); 58 super.onSaveInstanceState(outState); 59 } 60 @Override 61 public void onDestroyView() { 62 Log.d("test", "destroyFragment1"); 63 super.onDestroyView(); 64 } 65 66} 67 68

frag1.xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_main" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".MainActivity"> 9 10 <TextView 11 android:id="@+id/text_view1" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:textSize="30sp" 15 /> 16 17 <Button 18 android:id="@+id/button1" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:layout_margin="20dp" 22 android:text="tab1" 23 /> 24 25</LinearLayout>

Fragment2.java

1import android.support.annotation.Nullable; 2import android.support.v4.app.Fragment; 3import android.os.Bundle; 4import android.util.Log; 5import android.view.LayoutInflater; 6 7/* 8 * タブがクリックされた時に表示するFragment 9 */ 10public class Fragment2 extends Fragment { 11 12 TextView textView2; 13 Button button2; 14 15 16 @Override 17 public void onCreate(Bundle savedInstanceState){ 18 super.onCreate(savedInstanceState); 19 20 //addTabの際にBundleを渡す場合は、Bundleから値を取得する 21 //渡さない(nullを渡している)場合は、実装しなくてよい。そうでないとgetString("string")時にエラーが発生する 22 Bundle args = getArguments(); 23 String str = args.getString("string"); 24 } 25 26 @Nullable 27 @Override 28 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 29 View v = inflater.inflate(R.layout.frag2, null); 30 31 Log.d("debug", "frag2_OnCreateView"); 32 33 // text ------------------------------------------------------------------------------------ 34 textView2 = v.findViewById(R.id.text_view2); 35 textView2.setText("frag2_Hello"); 36 37 button2 = v.findViewById(R.id.button2); 38 button2.setOnClickListener(new View.OnClickListener() { 39 @Override 40 public void onClick(View v) { 41 textView2.setText("frag2_World"); 42 } 43 }); 44 return v; 45 } 46 @Override 47 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 48 super.onViewCreated(view, savedInstanceState); 49 Log.d("test", "createFragment2"); 50 } 51 52 @Override 53 public void onSaveInstanceState(Bundle outState) { 54 String i = "Hi"; 55 outState.putString("editText", i); 56 super.onSaveInstanceState(outState); 57 } 58 @Override 59 public void onDestroyView() { 60 Log.d("test", "destroyFragment2"); 61 super.onDestroyView(); 62 } 63}

frag2.xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 > 7 <TextView 8 android:id="@+id/text_view2" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:textSize="30sp" 12 /> 13 14 <Button 15 android:id="@+id/button2" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:layout_margin="20dp" 19 android:text="tab2" 20 /> 21 22</LinearLayout>

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

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

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

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

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

jimbe

2019/03/28 11:59

再現したいのですが, アクティビティ全体と各フラグメントと各レイアウトをご提示願えますか?
akai_kinomi

2019/03/29 02:29

ありがとうございます。追記しました。 字数制限の関係でimport等省略している部分があります。また、質問文も編集致しました。 漠然とした質問で申し訳ありませんが、よろしくお願い致します。
jimbe

2019/03/29 05:54

ありがとうございます.
guest

回答1

0

ベストアンサー

単純に申し上げれば, Fragment1 で TextView に表示する文字列を別途保存しておけば良いのではないでしょうか.
TextView 等の表示はあくまで『表示』ですので, データを再度表示したければ, データを保存しておく必要があると思います.

public class Fragment1 extends Fragment { TextView textView1; Button button1; private String message = "Frag1_Hello"; //textView1 に表示する文字列 (略) @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View v = inflater.inflate(R.layout.frag1, null); Log.d("Fragment1", "OnCreateView"); // text ------------------------------------------------------------------------------------ textView1 = v.findViewById(R.id.text_view1); textView1.setText(message); //メッセージを設定 button1 = v.findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //ボタンが押されたら message = "frag1_World"; //文字列を変更 textView1.setText(message); //変更したので(再)設定 } }); return v; } (略) }

投稿2019/03/29 05:52

編集2019/03/29 05:54
jimbe

総合スコア12545

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問